aspose file tools*
The moose likes Java in General and the fly likes How do iterate 2 list's using for-each loop(enhanced for loop) 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 do iterate 2 list Watch "How do iterate 2 list New topic
Author

How do iterate 2 list's using for-each loop(enhanced for loop)

jami siva
Ranch Hand

Joined: Oct 16, 2009
Posts: 60
I Have 2 list's called d and e, I want to iterate through these lists at the same time

List<Dept> d = new ArrayList<Dept>();
List<Emp> e = new ArrayLlist<Emp>();

And some elements are inserted into these lists.
I want to iterate these lists at the same time by using for-each loop, I don't know how to do this.
But I achieved this using old for loop.

for (int i=0; i<d.size(); i++) {
for(int j=0; j<e.size(); j++) {
if(i==j)
System.out.println("---"+e.get(i)+"---"+d.get(i));
}
}

same thing I want to achieve with for-each loop. Please don't ask why you want this?
I know to iterate through one list of elements like this.
for(Emp emp : e) {
System.out.println("---"+e);
}

How to achieve this? Is this possible in for - each loop or any alternative solutions for this.

Thanks
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

The code you're using basically does the same as this:

Only my implementation will perform much better. This can't be done with an enhanced for-loop.
This is because with the regular for loop you're comparing indexes which can't be done with
an enhanced for-loop. I also don't see why you would want that.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
jami siva
Ranch Hand

Joined: Oct 16, 2009
Posts: 60
Thanks Wouter for your quick reply,
Your regular for loop is much better than me.
I want for-each loop because of to avoid type-casting, specifying the size of the list and all at the time of Iteration.
If you have any Idea share with us.


Thanks
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Which casting? You're not casting anything. Anyway even if you where casting, an enhanced for-loop doesn't do anything like that. The loop only takes an Iterator and iterates that. Allowing you to access the elements of the data-source.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

You can also use explicit Iterators:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Wouter Oet wrote:Which casting? You're not casting anything.

One assumes that once the double-iteration problem is solved, he might want to do something more interesting than just print the values. This might well require accessing methods defined for the Dept and Emp classes.

Wouter Oet wrote:Anyway even if you where casting, an enhanced for-loop doesn't do anything like that. The loop only takes an Iterator and iterates that. Allowing you to access the elements of the data-source.

Ummm... an enhanced for loop also uses the generic type of the List (or more generally, Iterable) and allows us to treat elements from that List as instances of the appropriate type. It does this by inserting a cast, which we can't see in the source, but it's there nonetheless. It's what allows us to write

rather than
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Mike Simmons wrote:
Wouter Oet wrote:Which casting? You're not casting anything.

One assumes that once the double-iteration problem is solved, he might want to do something more interesting than just print the values. This might well require accessing methods defined for the Dept and Emp classes.
True but the list is already generic so casting isn't needed.

Mike Simmons wrote:
Wouter Oet wrote:Anyway even if you where casting, an enhanced for-loop doesn't do anything like that. The loop only takes an Iterator and iterates that. Allowing you to access the elements of the data-source.

Ummm... an enhanced for loop also uses the generic type of the List (or more generally, Iterable) and allows us to treat elements from that List as instances of the appropriate type. It does this by inserting a cast, which we can't see in the source, but it's there nonetheless. It's what allows us to write

rather than
Also true but I consider the casting to be done by the Iterator not the loop. In that way the enhanced for-loop is just syntactic sugar to iterate a data-source using an iterator. However that is debatable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do iterate 2 list's using for-each loop(enhanced for loop)
 
Similar Threads
hast table and unlimited looping
multidimensional array
create array of objects
Convert decimal value to binary
loop through arraylist of arrays