码迷,mamicode.com
首页 > 编程语言 > 详细

java对象数组

时间:2014-09-18 20:44:34      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:style   os   使用   java   ar   for   sp   on   c   

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;
}

}

java对象数组

标签:style   os   使用   java   ar   for   sp   on   c   

原文地址:http://blog.csdn.net/songjunyan/article/details/39377589

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!