# Is it possible to create a 2D arraylist?

• 06-15-2010, 02:34 AM
pahiker
Is it possible to create a 2D arraylist?
New here, moderate exposure to Java (not advanced, but not exactly a beginner either).

I've worked with 2D arrays before, but I didn't know if it was possible to create a 2D ArrayList. If so, how would it be defined?
• 06-15-2010, 02:44 AM
Norm
I would think you can create a multidimensional array of any type.
<type>[][] name = new <type>[3][]; // create 2 dim array of 3 elements
• 06-15-2010, 02:46 AM
Fubarable
Try it and see what happens. Please report back on what happens.
• 06-15-2010, 03:04 AM
pahiker
Quote:

Originally Posted by Norm
I would think you can create a multidimensional array of any type.
<type>[][] name = new <type>[3][]; // create 2 dim array of 3 elements

That's the thing, I can see the [3][], but I can't wrap my head around how to do the <type>. How would you get it to handle two dimensions? I'd think you'd have to have two <type> definitions, but how would you get them to work together.
• 06-15-2010, 03:04 AM
pahiker
Quote:

Originally Posted by Fubarable
Try it and see what happens. Please report back on what happens.

I have been trying, but just can't seem to figure out how to get it to work.
• 06-15-2010, 06:53 AM
m00nchile
Since this seems a specific enough question, I'll just provide the answer. Creating a 2d arraylist is indeed possible, just not in the way you're used to with arrays. You create an ArrayList, then you add ArrayLists to it, like this:
Code:

```ArrayList<ArrayList<Integer>> toparr = new ArrayList<ArrayList<Integer>>(); for(int i = 0; i < 10; i++)   toparr.add(new ArrayList<Integer>());```
Accessing the elemenst is also a little different than arrays:
Code:

`int a = toparr.get(2).get(1);`
This would fetch the ArrayList from toparr with the index 2, and then fetch the Integer from the contained ArrayList at index 1.
• 06-15-2010, 12:06 PM
pahiker
Hmm, not what I expected. What I have been doing is using an array as the data in an arraylist.

Object[] arrData = { 12345, "test" }

ArrayList<Object> arrList = new ArrayList<Object>();

May not be the exact code, but close.
• 06-15-2010, 12:20 PM
But, do want a 2D ArrayList or do you want an ArrayList of arrays?
• 06-15-2010, 12:26 PM
pahiker
I'm happy using what I have. This was more a question of curiosity. I couldn't find it addressed anywhere on java.sun, or in any other sites or books that address ArrayList. I tried a couple ways to do it on my own, with no success, so I thought I would ask some "experts" to see if it was even a possibility.

The "inner" array is a fixed-size array in my application (7 entries), but the "outer" array needs to be able to grow (hence the ArrayList). So the array-inside-an-ArrayList works fine.
• 06-15-2010, 12:38 PM
Then what you should be using is
Code:

```Object[] arrData = { 12345, "test" } ArrayList<Object[]> arrList = new ArrayList<Object[]>(); toparr.add(arrData);```
to, at least, make it a specific as possible.

But, since what you're working with is a "mixed" array, it would probably make more sense to write a class instead of using an array. I.E. if your "fixed" array is really something that contains, say, an integer, two string, a double, a date, and two more strings then what you should is instead of
Code:

```Obect[] a = new Object() { 0, "a", "b", 0.0, new Date(), "c", "d" }; ... a[0] ... a[1] ...```
you should be writing a class
Code:

```class Member {   int id;   String firstName;   String lastName;   double fee;   Date joinDate;   String preferredGroup;   String alias;   Member(int id, String firstName, String lastName, double ...) {     this.id = id;     this.firstName = firstName;     ...   }   public int getId() { return id; }   ... } Member m = new Member(0, "a", "b", 0.0, new Date(), "c", "d"); ... m.getId() ... m.getFirstName() ... // and use ArrayList<Member>```
• 06-15-2010, 01:08 PM
Norm
Quote:

how to do the <type>
I meant the <type> to be meta. It should be replaced with any valid java type. For example:
int[][] a2DimInt = new int[2][];
ArrayList[][] a2DimAL = new ArrayList[2][];
String[][] a2DimStr = new String[2][];
etc
• 06-15-2010, 01:59 PM
pahiker
Hmm, interesting. I'll give that some thought. Thanks.
• 06-15-2010, 05:49 PM
JosAH
Maybe a bit of Goedelization can be of any help: Goedelization is the mapping of more than one number to just one number. If no more than 2D matrixes are need the following mapping will do: given i and j the Goedelization could be (i << 16) | j for i and j < 2^16. A Map can come in handy too:

Code:

`Map<Integer, Double> map= new HashMap<Integer, Double>`
... and the following methods complete this trick:

Code:

```public Double get(int i, int j) {   return map.get((i << 16) | j); } public void set(int i, int j, double d) {   map.put((i << 16) | j, d); }```
When the get( ... ) method returns null there was no element stored at (i, j).

kind regards,

Jos