This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to access the elements of a 2 dimensional ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "How to access the elements of a 2 dimensional ArrayList" Watch "How to access the elements of a 2 dimensional ArrayList" New topic
Author

How to access the elements of a 2 dimensional ArrayList

simmi kamal
Greenhorn

Joined: Sep 07, 2007
Posts: 17
Hi all,

I have a class say XYZ which contains a method getSFInGroups() and this method returns an ArrayList within an ArrayList in the form of

[GroupName , ArrayList[EventName,SecurityFunctionVO]].

My question is how can I access the individual elements of this Arraylist,
like GroupName, EventName and the various fields inside the SecurityFunctionVO class which is a value object.

Thanks.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30580
    
154

Smita,
A 2D ArrayList isn't a built-in concept in Java like a 2D array is. A 2D ArrayList is really just an ArrayList that happens to contain other ArrayLists. As such, keep this in mind when accessing.

This is what you really have in your example:


Consider using a "real" object to store your values. Or a map if you have name/object pairs. Storing different types of objects in an ArrayList is confusing and hard to maintain. Java 5 introduces generics where you can say what type of ArrayList you have. In your case, you don't really have a type. You have a "bag" of objects that you happen to be storing in an ArrayList.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
simmi kamal
Greenhorn

Joined: Sep 07, 2007
Posts: 17
Thanks Jeanne.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Originally posted by Jeanne Boyarsky:
A 2D ArrayList isn't a built-in concept in Java like a 2D array is. A 2D ArrayList is really just an ArrayList that happens to contain other ArrayLists. As such, keep this in mind when accessing.

Technically, a 2D array isn't a built-in concept either - it's just an array of arrays.

String[][] is basically (String[])[], so an array or arrays of Strings. Even accessing it, lets say array[1][2] is nothing more than (array[1])[2] - first the value array[1] is retrieved, which is an array which then has its third element accessed. You can do the same thing with arrays retrieved through methods: "string".toCharArray()[2] is perfectly valid, and returns 'r'.

The only real difference is that there is a shortcut for creating. Whereas new ArrayList<ArrayList<String>> only creates the outer ArrayList, new String[x][y] also creates all inner arrays.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to access the elements of a 2 dimensional ArrayList