wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes how to create multidim arraylist and access it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to create multidim arraylist and access it" Watch "how to create multidim arraylist and access it" New topic

how to create multidim arraylist and access it

Lester D Jones

Joined: Mar 01, 2005
Posts: 15
Could anyone provide an example of how to create a two dimension arraylist and how to get its elements?


Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780

There is no emoticon for what I am feeling!
Joel McNary

Joined: Aug 20, 2001
Posts: 1817

Keep in mind that Lists (and arrays) are one-dimensional in Java. You can simulate higher-dimensional constructs by nesting one-dimsenional ones.

So, a two-dimensional array is a one-dimensional array of one-dimensional arrays:

Likewise, a two-dimensional list is a List of Lists:

(note that the use of generics makes the visualization easier)

Access of them is how you would expect. To get the second string of the third array, you first get the third array and then get the second String:

Lists (without Generics) are a little more complicated, but Generics cleans them up:

Hope that this helps.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Lester D Jones

Joined: Mar 01, 2005
Posts: 15
Using Jeff's example as a base I want to create a matrix with the parameters of a request object. Here is the snipet:

List<List<String>> matrix = new ArrayList<List<String>>();
while(reqParams.hasMoreElements()) {
List<String> paramNameCol = new ArrayList<String>();
List<String> paramCol = new ArrayList<String>();
paramName = (String)reqParams.nextElement();

I would expect to print the parameter but instead the paramenter is in matrix.get(1).get(0). It seems to me that I am adding a row instead of a column. What am I doing wrong?

thanks to all for the help

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38033
albert kao,
Your post was moved to a new topic.
It is sorta covered in the JavaRanch Style Guide.
subject: how to create multidim arraylist and access it
Similar Threads
About creating an array of ArrayList
Assigning arraylist variable to a method which returns of type AL
Parsing a text file to an arraylist
Implementing an arraylist using arrays