对象数组

来源:互联网 发布:html与js的区别 编辑:程序博客网 时间:2024/05/15 23:47

对象数组是包含有相同类型的一组类型的集合,对象数组是包含有一组对象的数组。


对象数组的格式为:

类名  对象数组名[]=new 类名[数组长度]

A b[]=new A[10];   //一个简单的小实例


例子:

class Person{private String name;public Person(String name){ this.name=name}public String getName()return this.name;};public class Redemo{public static void main(){Person b[]=new Person[3];//声明一个  含有三个元素的数组System.out.println("====数组实例化前====");for(int i=0;i<b.length;i++)System.out.println(b[i]+",");per[0]=new Person("Eason");per[0]=new Person("Jay");per[0]=new Person("Westlife");System.out.println("====数组实例化后====");for(int i=0;i<b.length;i++)System.out.println(b[i].getName()+",");}};

从以上代码的执行结果可以看出,声明对象数组,初值都是NULL,手动调用构造函数,在输出。


0 0
原创粉丝点击