您的位置:首页 > 编程语言 > Java开发

Java中对象数组的创建于使用

2015-07-20 17:22 579 查看

Java中创建对象数组的几种基本方法

首先我们需要创建一个class
class Student{
String name;
double score;
String num;

Student(String n,double s,String m){
name=n;
s=score;
num=m;
}

public static void printInfo(){
System.out.println(num+","+name+","+score);
}

}


接下来我们对此类进行数组的创建:

//1
Student stu[];<span style="white-space:pre">		</span>//声明数组。
stu=new Student [3];<span style="white-space:pre">	</span>//创建数组,这里是创建的一个引用的数组,每一个引用并没有确切的地址。
for(int i=0;i<3;i++){<span style="white-space:pre">	</span>//为数组创建对象,也就是说为创建的引用关联到确切的地址。
stu[i]=new Student();
}
//2
Student stu[]=new Student [3];
for(int i=0;i<3;i++){
stu[i]=new Student();
}
//3
Student stu[]=new Student{new Student(sjl,87,01),new Student(ljs,98,02),new Student(lls,92,03)};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: