(JAVA)数组与集合的初始化

1.数组类型:

2.1.基本类型:

int[] a=new int[b.length]; 
boolean[] a=new boolean[b.length];

2.2.对象类型:

linkNodes[] linkNode =new linkNodes[length];
for(int i=0;i<length;i++){
    linkNode[i]=new linkNodes();
}

 注意:通过数组定义来引用类,不会触发此类的初始化。也就是说,我们创建数组对象时分为两步:1.为对象数组分配空间。2.在分配的空间内创建对象实例。

2.集合类型:

ArrayList<Integer> array=new ArrayList<Integer>();                                     TRUE
List<Integer> list=new List<Integer>();                                               fALSE      
List<Integer> list=new ArrayList<Integer>();                                           TRUE
ArrayList<Integer> list=new ArrayList<Integer>();                                     fALSE      

HashMap<Integer,String> h=new HashMap<Integer,String>();

注意:List是一个接口,而ArrayList是List接口的一个实现类。所以new后面只能是ArrayList。

猜你喜欢

转载自blog.csdn.net/qq_24598059/article/details/89294644
今日推荐