Forums Register Login

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

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
You can also use explicit Iterators:
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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.
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
https://gardener-gift.com


reply
reply
This thread has been viewed 47911 times.
Similar Threads
create array of objects
multidimensional array
Convert decimal value to binary
loop through arraylist of arrays
hast table and unlimited looping
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:48:09.