35

I recently appeared for an interview in which the interviewer asked me a question regarding Arrays and ArrayList.

He asked me if an array of arrays can be multidimensional, then why is an ArrayList of ArrayList's not multidimensional?

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?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Lokesh Pandey
  • 1,739
  • 23
  • 50
  • 1
    Not all lists have to be the same size. In fact, some entries in the outer list may be null. – Michael Markidis May 09 '17 at 03:41
  • 12
    @MichaelMarkidis Not all arrays have to be the same size either. – Jacob G. May 09 '17 at 03:42
  • 7
    Actually, java doesn't have _true_ 2D arrays, they're implemented as arrays of arrays, but i can understand it when people say it is 2D... – Ousmane D. May 09 '17 at 03:43
  • I think it's a trick-question. An `ArrayList` is backed by an array, I don't see why it wouldn't be multidimensional. – Jacob G. May 09 '17 at 03:44
  • @JacobG. it's true, there's _no_ two-dimensional ArrayList's, this is actually quoted by Cay S. Horstmann. – Ousmane D. May 09 '17 at 03:45
  • 4
    @Aominè what do you mean by true 2D array ? – Lokesh Pandey May 09 '17 at 03:45
  • 2
    @Lokesh I don't know if you've used C# before but C# has _true_ 2D arrays, have a read [here](http://stackoverflow.com/questions/597720/what-are-the-differences-between-a-multidimensional-array-and-an-array-of-arrays) – Ousmane D. May 09 '17 at 03:49
  • @Aominè I don't know if I would consider it a `true 2D array`, but rather a square matrix. – Jacob G. May 09 '17 at 03:52
  • 1
    @JacobG. there is a huge difference between JAVAs jagged arrays and a 2D array. – Ousmane D. May 09 '17 at 03:56
  • 2
    This is one heck of a grey area - I could argue it either way. No doubt, the interviewer was just trying to see how well you understood this stuff. What it's called is kind of less useful in the real world. – Dawood ibn Kareem May 09 '17 at 04:00
  • @DawoodibnKareem yea, and he just kept the things around for about half and hour – Lokesh Pandey May 09 '17 at 04:40
  • 9
    "**A** two-dimensional `ArrayList`" implies there is *one* `ArrayList`. The same argument applies to arrays... but because Java has special syntax for them, people often think of multidimensional arrays as single objects (which they aren't really). – user253751 May 09 '17 at 06:18
  • 24
    In my opinion, this is a really stupid interview question. For all intents and purposes `List>` is a 2D array: you can visualise it as existing in two dimensions. If you wanted to be explicit about non-jaggedness, the word 'matrix' conveys that meaning much better. This was intentionally designed to trip you up. – Michael May 09 '17 at 10:05
  • If the interviewer just wants you to compare and contrast the data structures, it's a great question. If the interviewer thinks the question is a test of Java knowledge.. then see @Michael 's comment above. – Tony Ennis May 09 '17 at 22:17
  • Just for the record: please consider accepting the most helpful answer at some point ... – GhostCat May 25 '17 at 16:25

7 Answers7

25

Cay S. Horstmann has stated within his book Core Java for the impatient:

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.

due to the fact that ArrayLists 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.

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#?


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:

A multidimensional array need not have arrays of the same length at each level.

Community
  • 1
  • 1
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • 4
    Having sub-arrays of different length is not the exciting part. More interesting, the multi-dimensional array is an array of references, which might be `null` or point to the same array. In the case of `Object[][]…`, an array can even contain a reference to itself. – Holger May 09 '17 at 12:01
14

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:

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

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 does not have multidimensional arrays. 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. An array of arrays in C for example supports access with a single dereferencing operation so is closer to the multidimensional case.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
  • 9
    On which ground do you declare `array[index1][index2]` to be a single operation but `list.get(index1).get(index2)` to be two? In either case, I can split it into two operations, e.g. `tmp=array[index1]; result=tmp[index2];`… – Holger May 09 '17 at 12:33
  • @Holger I think the point here is that Java has no multidimensional array support. It has arrays of arrays - no different to lists of lists. I think in this case the interviewer is wrong - perhaps asking a leading question. – Boris the Spider May 11 '17 at 06:42
  • @Boris the Spider: I know, but this answer seems to see it different… – Holger May 11 '17 at 09:03
  • @Holger An array of arrays ( as opposed to Java's array of references to arrays ) can be implemented as a multiplication and single relative fetch. – Pete Kirkham May 11 '17 at 10:27
  • 1
    The question clearly is Java specific. If you are referring to non-Java arrays, you should indicate that in your answer. – Holger May 11 '17 at 11:03
11

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)!

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! 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[][]; 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!

On the other hand: there is a certain notion of "multi dimensional arrays", as for example the JVM specification explicitly mentions:

The first operand of the multianewarray instruction is the run-time constant pool index to the array class type to be created. 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. Note that the multidimensional array is just an object and so is loaded and returned by an aload_1 and areturn instruction, respectively.

Holger
  • 285,553
  • 42
  • 434
  • 765
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • The java type system does not support multidimensional arrays. It supports arrays of arrays, which is a different concept. – Pete Kirkham May 09 '17 at 09:26
  • @PeteKirkham So `Object[][]` is not a type different to `Object[]`, or `Object`? But I get your point ... and I hope my edits to my answer help here. – GhostCat May 09 '17 at 09:33
  • They are different types, `Object[][]` is an array of ( array of object ), whereas `Object[]` is an array of object, just as `int[]` and `int` are different types. And if java supported multidimensional arrays, `Object[,]` would be a different type again. See http://stackoverflow.com/questions/597720 for C# which does have multidimensional arrays. – Pete Kirkham May 09 '17 at 09:51
  • 1
    You can use specialized operations to let an object act like a multi-dimensional array, like the `multianewarray` instruction which allows an efficient implementation of, e.g. `Object[][] array=new Object[3][4];`, but it doesn’t prevent an entirely different usage, e.g. you could say `array[1]=null` and/or `array[0]=array[2];` after the instantiation, creating a data structure which doesn’t even remotely resemble a multi-dimensional array. – Holger May 09 '17 at 11:56
11

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:

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

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.

Jacob G.
  • 28,856
  • 5
  • 62
  • 116
  • Yea the size was fixed for multidimensional array. I also think because of the jagged nature arraylist can not be multidimensional. And yea you are right may be he is trying to confuse me, he kept things around for another half an hour. – Lokesh Pandey May 09 '17 at 05:02
  • 1
    I can effectively resize the sub-arrays of a multi-dimensional array, i.e. `array[index]=Arrays.copyOf(array[index], newSize);` – Holger May 09 '17 at 12:36
11

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. On the other hand, it certainly allows you to represent multidimensional structures via arrays and ArrayLists in the same way.

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.

Boann
  • 48,794
  • 16
  • 117
  • 146
  • 1
    +1 for the last sentence alone. These sorts of questions are really helpful to working out the depth of a candidate's technical knowledge. – Boris the Spider May 11 '17 at 06:43
4

An ArrayList is an implementation of List. It's a List that is implemented using arrays. 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. 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.

Samuel
  • 16,923
  • 6
  • 62
  • 75
0

Because it isn't dimensional at all. It is an object with an API. Any appearance of multi-dimensionality is provided by its API, but it is purely in the eye of the beholder. An array on the other hand is dimensional, and can therefore be multidimensional too.

user207421
  • 305,947
  • 44
  • 307
  • 483