public class Chushihua {
public static void main(String[] args){
//动态初始化:数组定义与分配空间、赋值分开。
Student[] student1;
student1=new Student[2];
student1[0]=new Student("xu",10);
student1[1]=new Student("wang",19);
//静态初始化:定义数组的同时为其分配空间和赋值。
Student[] student2=new Student[]{new Student("xu",10),new Student("wang",19)};
for(int i=0;i<student1.length;i++){
System.out.print(student1[i].name+" "+student1[i].age);
}
System.out.println("");
for(int i=0;i<student2.length;i++){
System.out.print(student2[i].name+" "+student2[i].age);
}
}
}
class Student{
public String name=null;
public int age=0;
public Student(String name,int age){
this.name=name;
this.age=age;
}
}
数组的初始化
最新推荐文章于 2021-06-07 10:22:06 发布