Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Regarding TypeSafe Iterator

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can..


Prints the following:


The second set starting with 'four' is a sorted list.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Krishna Potluri
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Legend has it that if you rub the right tiny ad, a genie comes out.
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic