java对象数组与普通数组有点不一样事实上最底层的原理是一样的我们使用对象数组一定要记得创创建对象不然会报空指针异常,
我已经遇到了两次了,每次都浪费 了非常长时间如今贴个初始化过程大家看看看好了以下一定要记得new对象不要仅仅在前面new数组
public class Student
{
private String username;
private int num;
public Student(String username, int num)
{
this.username = username;
this.num = num;
}
public static void main(String[] args)
{
Student s[] = new Student[10];
for (int i = 0; i < s.length; i++)
{
s[i] = new Student(i + "", i);
System.out.println(s[i]);
}
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public int getNum()
{
return num;
}
public void setNum(int num)
{
this.num = num;
}
@Override
public String toString()
{
return this.num + " " + this.username;
}
}