Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes loop through arraylist of arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "loop through arraylist of arrays" Watch "loop through arraylist of arrays" New topic
Author

loop through arraylist of arrays

J Brewer
Ranch Hand

Joined: Jul 10, 2006
Posts: 46
I just can't seem to get my head around how to iterate through an arraylist of arrays.

I get as far as, for example:
details - arraylist of object Details that has a field named Data, which is also an arraylist. Data stored arrays in the arraylist.
count - an outer loop i'm using to iterate through all details arraylists.


for (int i=0; i < details.get(count-1).getData().size(); i++){




this is as far as i get. Any suggestions?


Thanks,
[banghead]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61318
    
  66

Your problem is that you're biting off more than you can chew...

Let's take it one step at a time.

First, we know that we have a List of Detail objects, so let's set up a loop to iterate through those:



Now, inside the loop we have each detail object available, one at a time.

We also know that each detail contains a List of its own, so in order to iterate through that list we set up another loop:



You didn't say what type of object was in the data List, so I used "Whatever".

That help?

The upshot is, when you have nested lists/arrays, you need nested loops to iterate through them.
[ November 13, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61318
    
  66

By the way, if you are using JDK 1.5, the syntax can be considerably simplified through use of the new style of for loop.
J Brewer
Ranch Hand

Joined: Jul 10, 2006
Posts: 46
Thanks, I'll give it a try. I figured it would be nested loops but for some reason I couldn't get my head around it yesterday.
 
GeeCON Prague 2014
 
subject: loop through arraylist of arrays