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; } }
时间: 2024-11-09 06:43:45