class Student {
public String ID; public String name; public int score; public Student() {
this.ID = "";
this.name = "";
this.score = 0;
}
public String toString() {
return ID + " " + name + " " + String.valueOf(score);
}
}public class C {
public static int row; public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
row = sc.nextInt();
LinkedList<Student> l = new LinkedList<Student>();
Student[] st = new Student[N+1] ;
for (int i = 0; i < N; i++) {
System.out.println(st[i]);//为什么都输出null呢?而不是 0呢?(0前面是2个空格)
}
}
public String ID; public String name; public int score; public Student() {
this.ID = "";
this.name = "";
this.score = 0;
}
public String toString() {
return ID + " " + name + " " + String.valueOf(score);
}
}public class C {
public static int row; public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
row = sc.nextInt();
LinkedList<Student> l = new LinkedList<Student>();
Student[] st = new Student[N+1] ;
for (int i = 0; i < N; i++) {
System.out.println(st[i]);//为什么都输出null呢?而不是 0呢?(0前面是2个空格)
}
}
只是给数组分配了空间而已
分配空间了吗?应该仅仅是个声明吧!
class Student {
public String ID; public String name; public int score; public Student() {
this.ID = "";
this.name = "";
this.score = 0;
} public String toString() {
return ID + " " + name + " " + String.valueOf(score);
}
}public class Test {
public static int row; public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
row = sc.nextInt();
//LinkedList<Student> l = new LinkedList<Student>();
Student[] st = new Student[N + 1]; //仅仅起到声明作用。
for (int i = 0; i < N; i++) {
st[i] = new Student(); //需要初始化;
System.out.println(st[i]);
}
}
只是给数组分配了空间而已
其次,你问为什么不输出0,而输出null
是因为数组是Object,object数组初始化之前每个元素都为null,
假如,你这么写 int[] ii = new int[5];
System.out.println(ii[i]);
输出的是0,因为这是基本类型!
这就是object和基本类型的区别!