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();
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?