简体   繁体   English

为什么ArrayLists的ArrayList不是多维的?

[英]Why is an ArrayList of ArrayLists not multidimensional?

I recently appeared for an interview in which the interviewer asked me a question regarding Arrays and ArrayList . 我最近出现在面试中,面试官向我询问了有关ArraysArrayList的问题。

He asked me if an array of arrays can be multidimensional, then why is an ArrayList of ArrayList 's not multidimensional? 他问我数组的数组是否可以是多维的,那么为什么ArrayListArrayList不是多维的呢?

For example: 例如:

// Multidimensional
int[][] array = new int[m][n]; 

// Not multidimensional
ArrayList<ArrayList<Integer>> seq = new ArrayList<ArrayList<Integer>>(); 

Can anyone help me to understand this? 任何人都可以帮我理解这个吗?

Cay S. Horstmann has stated within his book Core Java for the impatient : Cay S. Horstmann在他的书“ 核心Java”中表示不耐烦

There are no two-dimensional array lists in Java, but you can declare a variable of type ArrayList<ArrayList<Integer>> and build up the rows yourself. Java中没有二维数组列表,但您可以声明ArrayList<ArrayList<Integer>>类型的变量并ArrayList<ArrayList<Integer>>构建行。

due to the fact that ArrayList s can expand and shrink and become jagged rather than multi-dimensional, one could say it is not a two-dimensional array, multi-dimensional meaning fixed rows and columns, hence why I have also stated within the comments Java doesn't have true multi-dimensional arrays but this is outside the scope of your question. 由于ArrayList可以扩展和缩小并且变得锯齿状而不是多维的事实,可以说它不是二维数组,多维意味着固定的行和列,因此我也在评论中说明了Java没有真正的多维数组,但这超出了您的问题的范围。

if you're curious as to why I said Java doesn't have true multi-dimensional arrays have a read at the differences between a multidimensional array and an array of arrays in C#? 如果你很好奇为什么我说Java没有真正的多维数组,那么读取C#中多维数组和数组数组之间的差异?


Just to make my answer clearer regarding whether Java has true multi-dimensional arrays or not, I did not say java doesn't have multi-dimensional arrays, I said Java doesn't have true multi-dimensional arrays and as expect the JLS has stated: 只是为了让我回答有关的Java是否有真正的多维数组更清晰与否,我并没有说Java没有多维数组,我说的Java没有真正的多维数组和期望JLS有声明:

A multidimensional array need not have arrays of the same length at each level. 多维数组不需要在每个级别具有相同长度的数组。

For the same reason the shopping bag I put all my spare shopping bags into is not a multidimensional shopping bag. 出于同样的原因,我把所有备用购物袋放进去的购物袋不是一个多维购物袋。

If I put a nut in one bag then put that bag in another bag, I have to perform two operations to get the nut. 如果我把一个坚果放在一个袋子里,然后将那个袋子放在另一个袋子里,我必须进行两次操作才能得到坚果。

If I instead put the nut in a two dimensional component tray, I can perform one operation to access it using two indices: 如果我将螺母放入二维组件托盘中,我可以使用两个索引执行一个操作来访问它:

组件托盘 source 资源

Similarly, there is a fundamental difference between a list of lists ( or array of arrays ) and a true two dimensional array - a single operation taking two indices is used to access the elements in a two dimensional array, two operations each taking one index are used to access the elements in a list of lists. 类似地,列表列表(或数组数组)与真正的二维数组之间存在根本区别 - 采用两个索引的单个操作用于访问二维数组中的元素,两个操作各自采用一个索引用于访问列表列表中的元素。

An ArrayList has a single index, so it has a rank of 1. A two dimensional array has two indices, its rank is 2. ArrayList具有单个索引,因此它具有等级1.二维数组具有两个索引,其等级为2。

note: by 'two dimensional array' I am not referring to a Java array of (references to) arrays, but a two dimensional array as found in other languages such as FORTRAN. 注意:'二维数组'我不是指数组(引用)的Java数组,而是指其他语言(如FORTRAN)中的二维数组。 Java does not have multidimensional arrays. Java没有多维数组。 If your interviewer was specifically referring to Java 'arrays of arrays' then I would disagree with them, as Java's int[][] defines an array of references to arrays of integers, and that requires two dereferencing operations to access the elements. 如果你的访问者专门提到Java'数组数组'那么我会不同意它们,因为Java的int[][]定义了一个对整数数组的引用数组,并且需要两个解引用操作来访问这些元素。 An array of arrays in C for example supports access with a single dereferencing operation so is closer to the multidimensional case. 例如,C中的数组数组支持使用单个解除引用操作进行访问,因此更接近于多维情况。

Looking at it from the other side: you can use lists in the very same way as "multi dimensional" arrays. 从另一方面看:它可以像“多维”数组一样使用列表。 You only have to replace array[row][column] with someList.get(row).get(column) ! 您只需someList.get(row).get(column) array[row][column]替换为someList.get(row).get(column)

And in the end, java arrays are implemented in similar ways: a two dim matrix is also just a one dim array of one dim arrays! 最后,java数组以类似的方式实现:两个dim矩阵也只是一个dim数组的一个暗淡数组! In other words: the difference is more on the surface, not rooted in deep conceptual reasons! 换句话说:差异在表面上更多,而不是根深蒂固的概念原因!

And to be really precise: the Java type system allows you to put down Object[][] so in that sense, it knows that type of Object[][] ; 而且要非常精确:Java类型系统允许你放下Object[][]所以在这个意义上,它知道Object[][] ; but as said, in reality, there are no multi-dimensional arrays; 但正如所说,实际上,没有多维数组; as Java sees that "two dim" thing as an array of references to arrays! 因为Java看到“两个昏暗”的东西作为数组的引用数组!

On the other hand: there is a certain notion of "multi dimensional arrays", as for example the JVM specification explicitly mentions: 另一方面:有一个“多维数组”的概念,例如JVM规范明确提到:

The first operand of the multianewarray instruction is the run-time constant pool index to the array class type to be created. multianewarray指令的第一个操作数是要创建的数组类类型的运行时常量池索引。 The second is the number of dimensions of that array type to actually create. 第二个是实际创建的数组类型的维数。 The multianewarray instruction can be used to create all the dimensions of the type, as the code for create3DArray shows. multianewarray指令可用于创建类型的所有维度,如create3DArray的代码所示。 Note that the multidimensional array is just an object and so is loaded and returned by an aload_1 and areturn instruction, respectively. 请注意,多维数组只是一个对象,因此分别由aload_1和areturn指令加载和返回。

I'm going to go out on a limb here and answer this one, however there is no correct answer for this broad question. 我将在这里站出来回答这个问题,但是对于这个广泛的问题没有正确的答案。

We first have to ask, what makes an array multidimensional? 我们首先要问,是什么使数组成为多维的?

I'm going to assume that your interviewer considers a multidimensional array one with a fixed size (as you've shown in your question), where it cannot be considered "jagged". 我将假设你的面试官考虑一个具有固定大小的多维数组(如你在问题中所示),它不能被认为是“锯齿状”。 According to Microsoft, a jagged array in C# is as follows: 根据微软的说法,C#中的锯齿状数组如下:

The elements of a jagged array can be of different dimensions and sizes. 锯齿状阵列的元素可以具有不同的尺寸和大小。

In Java, a multidimensional array is simply an array, where each element is also an array. 在Java中,多维数组只是一个数组,其中每个元素也是一个数组。 These arrays must be defined with a fixed size in order for elements to be indexed within them, but jagged arrays can be of different sizes, as stated above. 必须使用固定大小定义这些数组,以便在其中索引元素,但如上所述,锯齿状数组可以具有不同的大小。

An ArrayList is backed by an array; ArrayList由数组支持; however, the array expands when a certain number of elements are added to it. 但是,当向其添加一定数量的元素时,数组会扩展。 For this reason, the ArrayList could become jagged, and could be considered not to be multidimensional any longer. 出于这个原因, ArrayList可能会变得锯齿状,并且可能被认为不再是多维的。

Edit: After rereading everything over a few times, I'm sure that your interviewer was just trying to confuse you. 编辑:重读几遍后,我确信你的面试官只是想让你迷惑。 It honestly doesn't make sense for one data type (an array) to be multidimensional, and another data type (an ArrayList that uses an array) to not be multidimensional. 老实说,一种数据类型(数组)是多维的,而另一种数据类型(使用数组的ArrayList )不是多维的。

The interviewer's claim is nonsensical. 面试官的说法是荒谬的。

One can argue, as you see on this page, that Java does not have true multidimensional arrays, in which case it does not have multidimensional ArrayLists either. 正如您在本页中看到的那样,人们可以争辩说,Java没有真正的多维数组,在这种情况下,它也没有多维数组列表。 On the other hand, it certainly allows you to represent multidimensional structures via arrays and ArrayLists in the same way. 另一方面,它当然允许您以相同的方式通过数组和ArrayLists表示多维结构。

To define a major distinction between the two is fairly arbitrary and pointless. 定义两者之间的主要区别是相当武断和毫无意义的。

Possibly the interviewer was just trying to start a technical debate, to test your ability to explain the details. 可能面试官只是试图开始技术辩论,以测试你解释细节的能力。

An ArrayList is an implementation of List . ArrayListList的实现。 It's a List that is implemented using arrays. 它是使用数组实现的List The usage of arrays is an implementation detail. 数组的使用是一个实现细节。 The list interface doesn't support the concept of multi-dimensional lists therefore you wouldn't expect ArrayList to either. 列表界面不支持多维列表的概念,因此您不会期望ArrayList Further it isn't addressed as a use case of a traditional list data structure . 此外,它没有作为传统列表数据结构的用例来解决。

Arrays support multi-dimensionality because it's a language feature of Java. 数组支持多维度,因为它是Java的语言特性。

Because it isn't dimensional at all. 因为它根本不是维度的。 It is an object with an API. 它是一个带有API的对象。 Any appearance of multi-dimensionality is provided by its API, but it is purely in the eye of the beholder. 任何多维度的外观都是由它的API提供的,但它纯粹是在旁观者眼中。 An array on the other hand is dimensional, and can therefore be multidimensional too. 另一方面, 阵列 维度的,因此也可以是多维的。

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

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