新手小问题,大家来解决,呵呵

helloxx 2008-04-24 06:57:29
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个空格)
}
}
...全文
102 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
i_saw_you 2008-04-25
  • 打赏
  • 举报
回复
应该仅仅是个声明 没有初始化 当然是null
zuoguodang 2008-04-25
  • 打赏
  • 举报
回复
8楼分析的对,对于引用类型的实例默认是null,必须对其进行初始化
mike123hl 2008-04-25
  • 打赏
  • 举报
回复
8楼分析的很好
anqini 2008-04-24
  • 打赏
  • 举报
回复
首先什么都输出是因为你没有初始化数组,
其次,你问为什么不输出0,而输出null
是因为数组是Object,object数组初始化之前每个元素都为null,
假如,你这么写 int[] ii = new int[5];
System.out.println(ii[i]);
输出的是0,因为这是基本类型!
这就是object和基本类型的区别!
haoxiongok 2008-04-24
  • 打赏
  • 举报
回复
因为st[i]还没有初始化,还没有new
只是给数组分配了空间而已
Evenque 2008-04-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 believefym 的回复:]
因为st[i]还没有初始化,还没有new
只是给数组分配了空间而已
[/Quote]
goodmrning 2008-04-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 kevinchj 的回复:]
来迟了……
引用 1 楼 believefym 的回复:
因为st[i]还没有初始化,还没有new
只是给数组分配了空间而已

分配空间了吗?应该仅仅是个声明吧!


Java code
class Student {
public String ID;

public String name;

public int score;

public Student() {
this.ID = "";
this.name = "";
this.score = 0;
}

public String toString() {

[/Quote]
正解,要先声明后实例化.
kevinchj 2008-04-24
  • 打赏
  • 举报
回复
关于数组,可以参考类似http://java.chinaitlab.com/base/525802.html的一类文章。
kevinchj 2008-04-24
  • 打赏
  • 举报
回复
来迟了……
[Quote=引用 1 楼 believefym 的回复:]
因为st[i]还没有初始化,还没有new
只是给数组分配了空间而已
[/Quote]
分配空间了吗?应该仅仅是个声明吧!


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]);
}
}
helloxx 2008-04-24
  • 打赏
  • 举报
回复
I see
thx
believefym 2008-04-24
  • 打赏
  • 举报
回复
因为st[i]还没有初始化,还没有new
只是给数组分配了空间而已

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧