Stack<String> sk = new Stack<String>();
sk.push("Hello");
sk.push("Hello1");
sk.push("Hello2");
There are two ways i am iterating this Stack Object.
for(String s : sk){
System.out.println("The Values of String in SK" +sk);
}
// Way two..
Iterator<String> it=sk.iterator(开发者_如何学C);
while(it.hasNext())
{
String iValue=(String)it.next();
System.out.println("Iterator value :"+iValue);
}
- What is the difference between these two?
- Any Advantage if i choose one among them?
- Which is the preferred way of iterating?
The Iterator
has the advantage that you can remove elements from the collection while iterating over it. Foreach may throw ConcurrentModificationExceptions
there.
If you don't need that, I prefer the former way because it is easier to read.
1. What is the difference between these two?
Not much. The for-each loop construct actually relies on the iterator behind the curtains.
Further reading:
- The For-Each Loop, official documentation / tutorial
2. Any Advantage if i choose one among them?
Mostly readability I would assume.
(If you need to access the iterator.remove()
method, then obviously you would need to go with the explicit Iterator
approach.) However, keep in mind that it's an optional operation and may not be supported by the underlying Stack
implementation you're using.
Besides, the point of a Stack
structure is that you don't remove elements in the middle.
3. Which is the preferred way of iterating?
Use the for-each approach, if that works for you.
with the Iterator
you can prevent a java.util.ConcurrentModificationException
, when you modify the list while looping over them. Iterator.remove()
From Effective Java, by Joshua Bloch:
The
for
loop, in both its traditional andfor-each
forms, allows you to declare loop variables, limiting their scope to the exact region where they’re needed. (This region consists of the body of the loop as well as the initialization, test, and update preceding the body.)Therefore, preferfor
loops towhile
loops, assuming the contents of the loop variable aren’t needed after the loop terminates.
To make it short:
the
for-each
loop is always type safethe
for-each
loop is less prone to copy/paste errors (copy/paste iterator1 twice in your code!)the
for-each
loop allows you to minimize the scope of local variablesthe
for-each
loop is easier to read!
In summary, prefer for-each
loops to while
loops when you do not explicitly need a while
loop.
Choosing between For each and an Iterator
is just a matter of convenience. For each loop looks a bit cryptic especially to those who are used to the traditional for loop. The downside with Iterator
s, however, is that you've to cast the Object
from the next()
to the appropriate type, although your collection is not typed. With For each, everything is happening behind the scenes.
What is the difference between these two?
Nothing. Except for the extra code
Any Advantage if I choose one among them?
Yes. If you use the newer one (foreach), you have fewer lines and are therefore easier to read.
Which is the preferred way of iterating?
The foreach loop
You may want to read Nuances of the Java 5.0 for-each Loop and the official for-each loop tutorial
精彩评论