wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding TypeSafe Iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding TypeSafe Iterator" Watch "Regarding TypeSafe Iterator" New topic
Author

Regarding TypeSafe Iterator

Krishna Potluri
Greenhorn

Joined: Apr 20, 2007
Posts: 19
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.
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
Yes, you can..


Prints the following:


The second set starting with 'four' is a sorted list.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

Can I use the same Iterator object to print the elements in the List twice?


I would say, "NO". "Iterator object is for one time use!"


M Krishnan says,
Yes, you can..


In your example, if you see carefully, you are getting two iterator
objects to traverse from the list. As said, Iterator object is for one time
use. We can use the phrase "USE AND THROW" for the iterator.

Each time you need to traverse from the List you create a new iterator, means calling list.iterator(); gives you new iterator object.



Regards,
cmbhatt
[ April 26, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This is rather beyond the scope of SCJP.

However, you could play with ListIterator which is a bidirectional Iterator.
So, when you get to the end of the list you reset the ListIterator by going backwards until you get to the point where hasPrevious() returns false. Then you can go forwards again. Rather inefficient methinks, but it "solves" the stated problem.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Krishna Potluri
Greenhorn

Joined: Apr 20, 2007
Posts: 19
Hi Chandra,

From your response I understood that, Iterator object can be used only once for traversing the Collection. If i need to traverse the same collection 2nd time then i need to create one more Iterator object.

Thanks for clearing my doubt.

Thanks and Regards,
Krishna.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Regarding TypeSafe Iterator