Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to create multidim arraylist and access it

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could anyone provide an example of how to create a two dimension arraylist and how to get its elements?

thanks

ldj
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Lester D Jones
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79530
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
albert kao,
Your post was moved to a new topic.
 
There's a way to do it better - find it. -Edison. A better tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic