I know that I can add a dimension to an array by adding another [] beside it. But can I have more than one Dimension in a java.util.ArrayList? How might I accomplish this?
-
Duplicate of http://stackoverflow.com/questions/10866205/2-dimensional-array-list – zardosht May 18 '14 at 20:27
3 Answers
List<ArrayList<Integer>> twoDArrayList = new ArrayList<ArrayList<Integer>>();
@rgettman's answer gets the job done, however there are a few caveats to take note of:
Caveat 1: dimensions
In the most common use case the dimensions of the array are pre-defined, for instance:
int[][] array = new int[5][6];
In that case, the array will be of a "rectangular" form of the dimensions defined:
0 1 2 3 4 5
0 [][][][][][]
1 [][][][][][]
2 [][][][][][]
3 [][][][][][]
4 [][][][][][]
As suggested by another member in the comments below, there's more to it. A "two-dimensional array" is merely an array of other arrays, and the line of code above is short-hand for:
int[][] array = new int[5][];
array[0] = new int[6];
array[1] = new int[6];
array[2] = new int[6];
array[3] = new int[6];
array[4] = new int[6];
Alternatively, the child arrays could be instantiated with different sizes, in which case the "data shape" would no longer be rectangular:
int[][] array = new int[5][];
array[0] = new int[2];
array[1] = new int[4];
array[2] = new int[1];
array[3] = new int[6];
array[4] = new int[3];
0 1 2 3 4 5
0 [][]
1 [][][][]
2 []
3 [][][][][][]
4 [][][]
Using the ArrayList<ArrayList<Integer>>
approach will result in a "list of lists" where the length of all lists involved will grow as a result of the operations performed.
There is no shorthand to pre-define the dimensions. The child lists must be inserted into the master list, and data elements must be then inserted into the child lists. The shape of the data would thus resemble the second example:
0 [][] <- list with 2 elements
1 [][][][] <- list with 4 elements
2 [] ...and so on
3 [][][][][][]
4 [][][]
Caveat 2: default values of data
Arrays allow the use of primitive data types (such as "int"), as well as their boxed counterparts (such as "Integer"). These behave differently, when it comes to default values of elements.
int[][] array1 = new int[5][6]; // all elements will default to 0
Integer[][] array2 = new Integer[5][6]; // all elements will default to null
Lists (like all other collections) only allow the use of boxed types. And therefore, while it's possible to pre-define the length of a list, the default value of its elements will always be null.
List<Integer> = new ArrayList<Integer>(10); // all elements will default to null

- 665
- 5
- 9
-
-
@paulkore, how to iterate if we have two dimensional `ArrayList<>` or `List<>` as shown above ? – Paresh May 08 '15 at 05:03
-
4@paulkore, For the record, it should be noted that a two dimensional array (if created a row at a time) can be a non-rectangular "ragged" array, much like your list example. – May 08 '15 at 23:29
-
@paulkore: What user4229245 mentions is a really important fact! You simply state wrong things that confuse people – h345k34cr Sep 27 '16 at 09:58
-
@h345k34cr, thanks for pointing this out. I have now revised the answer to hopefully better reflect the case. I must say, that I find your comment unnecessarily harsh, as my intent was to share my best understanding at the time, contrary to "stating wrong things to confuse people". I hope that you'd opt for a friendlier approach in your future comments. – paulkore Sep 28 '16 at 14:44
Yes, it's possible. Just have the elements of your ArrayList
also be ArrayLists
.
ArrayList<ArrayList<Integer>> twoDArrayList = new ArrayList<ArrayList<Integer>>();
This would work with not just ArrayLists
, but other collection types as well.

- 176,041
- 30
- 275
- 357
Yes, you can! In a regular array, when you add the second pair of braces, you are creating a normal array that stores Objects of type array. You can just do the same thing here, making the ArrayList hold things of type ArrayList: ArrayList<ArrayList<Object>> list = new ArrayList<ArrayList<Object>>();

- 11
- 1