Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
jami siva
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jami siva
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use explicit Iterators:
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic