Java数组

Java数组申明和静态初始化

class Person_2{
	private String name;
	public Person_2(String name){
		this.name = name;
	}
	
	public String getName(){
		return this.name;
	}
}

public class array_2 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
//		Person_2 per[] = new Person_2[3];
		Person_2 per[] = {new Person_2("zhangsan"),new Person_2("lisi"),new Person_2("wangwu")};
		System.out.println("-------before-------");
		for (int i=0;i<per.length;i++){
			System.out.println(per[i]);
		}
//		per[0]=new Person_2("zhangsan");
//		per[1]=new Person_2("lisi");
//		per[2]=new Person_2("wangwu");
		for (int i=0;i<per.length;i++){
			System.out.println(per[i].getName());
		}
	}

}

 

动态初始化

class Person_2{
	private String name;
	public Person_2(String name){
		this.name = name;
	}
	
	public String getName(){
		return this.name;
	}
}

public class array_2 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Person_2 per[] = new Person_2[3];
		
		System.out.println("-------before-------");
		for (int i=0;i<per.length;i++){
			System.out.println(per[i]);
		}
		per[0]=new Person_2("zhangsan");
		per[1]=new Person_2("lisi");
		per[2]=new Person_2("wangwu");
		for (int i=0;i<per.length;i++){
			System.out.println(per[i].getName());
		}
	}

}

 

你可能感兴趣的:(Java数组)