简体   繁体   English

ArrayList的ArrayList是否类似于java中的2D数组?

[英]Is ArrayList of ArrayList similar to a 2D array in java?

Is an ArrayList<ArrayList<Integer>> numbers; ArrayList<ArrayList<Integer>> numbers; like a 2D array of ints int[][] numbers; int[][] numbers;的二维数组int[][] numbers; ? Or are these stored completely different from one another? 或者这些存储完全不同?

It is a structure with two dimensions, so it is [][]-like, yet there is one very important difference: if you allocate a two-dimensional array in one step you'll get same size in the second dimension for all elements of the first dimension: 它是一个具有两个维度的结构,因此它是[] [] - 但是有一个非常重要的区别:如果你在一个步骤中分配一个二维数组,你将在第二个维度中为所有元素获得相同的大小第一个维度:

int[][] arrayOfInts = new int[5][4];

for (int[] second : arrayOfInts) {
   System.out.println(second.length);
}

prints 5 times a "4". 打印5次“4”。

Using ArrayList of ArrayLists all elements of the second dimension may have different size. 使用ArrayLists的ArrayList,第二维的所有元素可以具有不同的大小。

As pointed out by jlordo: an array of ints may also have the second dimension of different lengths if it is created dynamically: 正如jlordo所指出的那样:如果动态创建,则int数组也可能具有不同长度的第二维:

int[][] anotherArray = new int[5][];

for (int i=0; i<5; i++) {
  anotherArray[i] = new int[i];
}

in which case a NullPointerException can be throws if the second dimension were accessed before being intialized, like: 在这种情况下,如果在初始化之前访问第二个维度,则可以抛出NullPointerException,如:

int[][] yetAnotherArray = new int[5][];
System.out.println(yetAnotherArray[2][3]);

The other difference: after allocating a int[x][y] the memory for all its elements in both dimension is allocated from the very first moment. 另一个区别是:在分配int [x] [y]之后,两个维度中所有元素的内存从第一刻开始分配。 In ArrayList of ArrayLists the memory needed for the lists is allocated, but the memory needed for its elements will be used not before you create their content. 在ArrayLists的ArrayList中,将分配列表所需的内存,但不会在创建内容之前使用其元素所需的内存。 So a similar code as before will print nothing , because at the beginning there will not be even a single element in the first ArrayList. 因此,与之前类似的代码将不会打印任何内容 ,因为在开始时,第一个ArrayList中甚至不会有单个元素。

In order to have the second dimension you first have to create all ArrayLists of the second dimension: 要获得第二个维度,首先必须创建第二个维度的所有ArrayLists:

ArrayList<ArrayList<Integer>> arrayOfArrays = new ArrayList<ArrayList<Integer>>();
for (int i=0; i < 5; i++) {
    arrayOfArrays.add(new ArrayList<Integer>();
}

Further on the accessing side: 进一步在访问方面:

int[][] arrayOfInts = new int[5][4];
System.out.println(arrayOfInts[2][3]);

prints 0 because all of the memory is already allocated. 打印0,因为已经分配了所有内存。 The access is safe both in addressing the dimensions and its value, because it is of a primitive type. 访问在处理维度及其值时是安全的,因为它是原始类型。

ArrayList<ArrayList<Integer>> arrayOfArrays = new ArrayList<ArrayList<Integer>>();
for (int i=0; i < 5; i++) {
    arrayOfArrays.add(new ArrayList<Integer>();
}
System.out.println(first.get(2).get(3));

throws ArrayOutOfBoundsException. 抛出ArrayOutOfBoundsException。 You have to check the size of the elements before accessing them. 在访问元素之前,您必须检查元素的大小。

Now there is also one important difference between int[][] and Integer[][]: primitive types always have values, so after allocating int[4][5] you'll have 0 for unitialized elements. 现在int [] []和Integer [] []之间也有一个重要的区别:原始类型总是有值,所以在分配int [4] [5]之后,对于单元化元素你将得到0 Integer[4][5] contain objects, so unitialized elements will have null instead. 整数[4] [5]包含对象,因此单元化元素将为null

It is similar to Integer[][] , which is somewhat different to int[][] . 它类似于Integer[][] ,它与int[][]有些不同。

Also ArrayList provides additional facilities over what an array does, such as the ability to grow dynamically, and manage separate notions of size and capacity . 此外, ArrayList还提供了与阵列相关的额外功能,例如动态增长的能力,以及管理大小容量的单独概念。

Not fully sure but the memory allocation would be different in both cases. 不完全确定,但两种情况下的内存分配都不同 The primitive array int[][] will be allocated in stack while the ArrayList<ArrayList<Integer>> will be allocated in Heap . 原始数组int[][]将在堆栈中分配,而ArrayList<ArrayList<Integer>>将在Heap中分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM