当前位置: 代码迷 >> J2SE >> 其实是个小疑点。求解决
  详细解决方案

其实是个小疑点。求解决

热度:96   发布时间:2016-04-24 01:51:42.0
其实是个小问题。。求解决
Java code
public class Student{    private String id;    private String name;    private int score;        public Student()    {            }    public Student(String id,String name,int score)    {        this.id = id;        this.name = name;        this.score= score;    }        public void setRecord(String id,String name,int score)    {        this.id = id;        this.name = name;        this.score = score;    }        public int getRecord(String id)    {        if(id.equals(this.id))            return this.score;        return 0;    }    public static void main(String[] args)    {            Student[] students = new Student[5];        students[0].setRecord("10", "wu", 51);        students[1].setRecord("11", "we", 52);        students[2].setRecord("12", "ww", 53);        students[3].setRecord("13", "wq", 54);        students[4].setRecord("14", "wt", 55);        int s = 0;        for (int i = 0; i < 5; i++)        {            s= students[i].getRecord("10");        }        System.out.println("" + s);    }}



这个代码为什么会出错呢?要怎样解决?请指导,给出正确代码。。

------解决方案--------------------
Student[] students = new Student[5];
students[0].setRecord("10", "wu", 51);
你只是创建的放5个Student类型的数组,分配了空间!但是里面放的值都是null;

students[0] = new Student();
  相关解决方案