ArrayList的ArrayList是否类似于java中的2D数组? [英] Is ArrayList of ArrayList similar to a 2D array in java?

查看:155
本文介绍了ArrayList的ArrayList是否类似于java中的2D数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


ArrayList< ArrayList< Integer>>数字; 就像一个二维数组的整数 int [] []数字;
或者这些存储完全不同吗?

Is an ArrayList<ArrayList<Integer>> numbers; like a 2D array of ints 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);
}

打印5次4。

使用ArrayLists的ArrayList,第二个维度的所有元素可能具有不同的大小。

Using ArrayList of ArrayLists all elements of the second dimension may have different size.

正如jlordo指出的那样:如果是动态创建的,则int数组也可能具有不同长度的第二个维度:

As pointed out by jlordo: an array of ints may also have the second dimension of different lengths if it is created dynamically:

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

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

在这种情况下,如果在被访问之前访问了第二个维度,则可以抛出NullPointerException初始化,如:

in which case a NullPointerException can be throws if the second dimension were accessed before being intialized, like:

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

另一个区别:为int [x] [y]分配了所有元素的内存这两个维度都是从第一时刻开始分配的。在ArrayLists的ArrayList中,将分配列表所需的内存,但不会在创建内容之前使用其元素所需的内存。因此,与之前类似的代码将打印 nothing ,因为在开始时,第一个ArrayList中甚至不会有单个元素。

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. 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. 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.

In为了拥有第二个维度,首先必须创建第二个维度的所有ArrayLists:

In order to have the second dimension you first have to create all ArrayLists of the second dimension:

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

进一步在访问方:

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

打印0因为已经分配了所有内存。访问在寻址维度及其值时都是安全的,因为它是原始类型。

prints 0 because all of the memory is already allocated. 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));

抛出ArrayOutOfBoundsException。在访问元素之前,你必须检查它们的大小。

throws ArrayOutOfBoundsException. You have to check the size of the elements before accessing them.

现在int [] []和Integer [] []之间也有一个重要区别:原始类型alwa ys有值,所以在分配int [4] [5]之后,对于单元化元素你将有 0 。整数[4] [5]包含对象,因此单位元素将具有 null

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. Integer[4][5] contain objects, so unitialized elements will have null instead.

这篇关于ArrayList的ArrayList是否类似于java中的2D数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆