user2097804
user2097804

Reputation: 1132

Two Dimensional ArrayList

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?

Upvotes: 19

Views: 58591

Answers (3)

paulkore
paulkore

Reputation: 665

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

Upvotes: 25

user3260450
user3260450

Reputation: 11

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>>();

Upvotes: 0

rgettman
rgettman

Reputation: 178333

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.

Upvotes: 24

Related Questions