会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132808个问题

package integre.fang.com;
/**
 * 自己手写得包装类
 *
 * */
public class MyInteger01 {
    private int value;
    public static final int LOW=-128;
    public static final int HIGH=127;
    private static MyInteger01[] myInteger01;

    static {
        for (int i=LOW;i<=HIGH;i++){
          myInteger01[i+128]=MyInteger01.getMyinteger(i);//核心得点在这里,有点迷糊
        }
    }

    public  MyInteger01(int a) {
        this.value = a;
    }

    public static MyInteger01 getMyinteger(int a){
        if (LOW<=a && a<=HIGH){
            return myInteger01[a+128];
        }
            return new MyInteger01(a);

    }

    public static void main(String[] args) {
        int a=100;
        int b=100;
        MyInteger01 m1=new MyInteger01(300);
        MyInteger01 m2=new MyInteger01(300);
        MyInteger01 m3=new MyInteger01(100);
        MyInteger01 m4=new MyInteger01(100);
        System.out.println(m1==m2);
        System.out.println(m3==m4);
        //System.out.println(MyInteger01.getMyinteger(a)==MyInteger01.getMyinteger(b));
        //System.out.println(MyInteger01.getMyinteger(a).equals(MyInteger01.getMyinteger(b)));

        int c=1000;
        int d=1000;
        //System.out.println(MyInteger01.getMyinteger(c)==MyInteger01.getMyinteger(d));
        //System.out.println(MyInteger01.getMyinteger(c).equals(MyInteger01.getMyinteger(d)));
    }

}

Exception in thread "main" java.lang.ExceptionInInitializerError

老师帮忙看看哪里出错了?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 1726楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 1730楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 1732楼

老师,电话本项目作业,我在operate类的addOpration中添加了ObjectOutputStream,

public void addOperation() {
    TelNoteRegex telNoteRegex=new TelNoteRegex();
    String name=telNoteRegex.nameValidate();
    String age=telNoteRegex.ageValidate();
    String sex=telNoteRegex.sexValidate();
    String telNum=telNoteRegex.telNumValidate();
    String address=telNoteRegex.addressValidate();
    Person person=new Person(name,age,sex,telNum,address);
    this.list.add(person);
    person.setId(this.list.size());
    ObjectOutputStream oos=null;
    try {
        oos=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("d:/person.txt",true)));
        oos.writeObject(person);
        oos.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
            if (oos!= null) {
                oos.close();
            }
        }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

然后在Operate类创建了一个ObjectInputStream方法,

/**
 * 对象输入流
 * 初始化list
 */
public void ObjectInputStream(){
    ObjectInputStream ois=null;
    try{
        ois=new ObjectInputStream(new BufferedInputStream(new FileInputStream("d:/person.txt")));
        List list=(List<Person>)ois.readObject();
        this.list=list;
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        try{
            if (ois!=null){
                ois.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

再在app类的start方法里调用operate的ObjectInputStream方法,初始化list内的信息,以便下次打开程序时读取之前存储的信息。

public void start(){
    Menu menu=new Menu();
    TelNoteRegex regex=new TelNoteRegex();
    Operate operate=new Operate();
    operate.ObjectInputStream();//初始化Operate中的list
    while (true) {
        menu.mainMenu();
        int item=regex.menuItemValidate(1,6);
        switch (item){
            case 1:operate.addLogic();break;
            case 2:operate.searchLogic();break;
            case 3:operate.modifyLogic();break;
            case 4:operate.deleteLogic();break;
            case 5:operate.orderLogic();break;
            case 6:System.exit(0);  //结束虚拟机

程序运行后有如下报错。

java.io.EOFException
	at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2638)
	at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3113)
	at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:853)
	at java.io.ObjectInputStream.<init>(ObjectInputStream.java:349)
	at com.wxc.Operate.ObjectInputStream(Operate.java:159)
	at com.wxc.App.start(App.java:28)
	at com.wxc.App.main(App.java:18)

不知道这个思路问题出在哪里。

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 1735楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637