Hi,
Can I use the same Iterator object to print the elements in the List twice?
Is there any way to do like that.
I know that once we traverse the collection using the Iterator object, IS IT POSSIBLE TO TRAVERSE THE SAME COLLECTION OBJECT AGAIN USING SAME Iterator object to print the elements..
**************************************************************************
// Here my doubt is: Is there a way to use the same Iterator
// to traverse the same collection twice by giving the same output.
***************************************************************************
My Code is as follows:
___________________________________________________________________________
package com.kris.tigerv.ch2;
import java.util.List;
import java.util.LinkedList;
import java.util.Collections;
import java.util.Iterator;
import org.apache.log4j.Category;
public class TypeSafeListWithIterator {
public static void main(
String[] args) {
List<String> list = new LinkedList<String>();
list.add("Sri");
list.add("Krishna");
list.add(new String("Prasad"));
list.add("Potluri");
Category cat = Category.getInstance(TypeSafeListWithIterator.class);
cat.info("...List Elements Before Sort...");
/* for(String s:list){
cat.info(s+" ");
} */
// Iterator
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()) {
cat.info(iterator.next());
}
Collections.sort(list);
cat.info("...List Elements After Sort...");
/* for(String s: list){
cat.info(s);
} */
// Here my doubt is: Is there a way to use the same Iterator
// to traverse the same collection twice by giving the same output.
Iterator<String> iterator1 = list.iterator();
while(iterator1.hasNext()){
cat.info(iterator1.next());
}
}
}
___________________________________________________________________________
Thanks In Advance,
Kris.