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.
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.
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.
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.
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.
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
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 by:autobot
Do you want ants? Because that's how you get ants. And a tiny ads:
a bit of art, as a gift, the permaculture playing cards