Thread: Does a 2D Arraylist exist?

1. Member
Join Date
Feb 2013
Posts
2
Rep Power
0

Does a 2D Arraylist exist?

I have created 2 regular 1-D Arrays but I want to stick them all into 1 ArrayList. Is this even possible. I want to tidy up the whole thing if possible. I am new to Java and this is for a homework assignment. My three arrays are all Integer Arrays so the ArrayList of Arrays would be of Integers if that makes any sense to anyone. Any help would be greatly appreciated!

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

Re: Does a 2D Arraylist exist?

Java Code:
```List<List<Integer>> array2D = new ArrayList<List<Integer>>();
...
// populate the list of lists
...
for (List<Integer> list : array2D) {
for (Integer i : list) {
System.out.println(i);
}
}

// to get individual elements, you would need to make multiple get calls.

int val = array2D.get(4).get(3);  // could be row 4, col 3 or row 3, col 4 depending on how you construct the list(s).```
Last edited by jim829; 02-23-2013 at 11:45 PM.

3. Re: Does a 2D Arraylist exist?

Moved from a staff-only section.

db

4. Member
Join Date
Feb 2013
Posts
2
Rep Power
0

Re: Does a 2D Arraylist exist?

Thank you jim829! It toke me a little to wrap myself around it but It works!

By any chance do you know if it's possible to use an ArrayList of Integers as the parameter in a Random generator?

5. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

Re: Does a 2D Arraylist exist?

I am not certain I know what you are asking. You can certainly pass a List or even List of Lists as an argument to a method which populates the list with random numbers.

Jim

6. Senior Member
Join Date
Jan 2009
Location
CA, USA
Posts
271
Rep Power
12

Re: Does a 2D Arraylist exist?

I often use a "HashGrid" class that I made, that is essentially a HashMap<Point, T> with a few extra gets and puts for convenience. Doesn't necessarily work the same way as a 2d array, but I found it quite useful for a number of my projects.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•