Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two Dimensional ArrayList

Tags:

java

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?

like image 454
user2097804 Avatar asked Mar 08 '13 01:03

user2097804


People also ask

Can ArrayList be two-dimensional?

Creating a multidimensional ArrayList often comes up during programming. In many cases, there is a need to create a two-dimensional ArrayList or a three-dimensional ArrayList. In this tutorial, we'll discuss how to create a multidimensional ArrayList in Java.

Can you make an ArrayList of 2D arrays?

ArrayList of arrays can be created just like any other objects using ArrayList constructor. In 2D arrays, it might happen that most of the part in the array is empty.

How do you add values to a two-dimensional ArrayList in Java?

<ArrayList_name>. add(Object element) : It helps in adding a new row in our existing 2D arraylist where element is the element to be added of datatype of which ArrayList created. <ArrayList_name> . add(int index, Object element) : It helps in adding the element at a particular index.


2 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
like image 69
paulkore Avatar answered Oct 17 '22 15:10

paulkore


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.

like image 37
rgettman Avatar answered Oct 17 '22 13:10

rgettman