File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Spring in Action this week in the Spring 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
Author

how to create multidim arraylist and access it

Lester D Jones
Greenhorn

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?

thanks

ldj
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780


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

Joined: Aug 20, 2001
Posts: 1821

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
Greenhorn

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();
paramNameCol.add(paramName);
paramCol.add(request.getParameter(paramName));
matrix.add(paramNameCol);
matrix.add(paramCol);
}
System.out.println(matrix.get(0).get(1));

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

ldj
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
albert kao,
Your post was moved to a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to create multidim arraylist and access it