iterator problem...new to iterator

hi all...i got a problem when i am reading my java text book ..there are two types of iterators implemented from my java text book but they perform the same purpose....so which one is better? or in more specify...What are the advantages and disadvantages of each of these two types of iterator?

//Version 1 simple iterator for our List.

publicclass List{

finalstaticint DEFAULT_COLLECTION_SIZE = 25;

private Listable[] theCollection;// collection of elements that have an ID

// and that implements "getID()" of the IDable interface

privateint numberOfElements;// current number of elements

privateint myIterator;// a very simple implementation of an iterator

// Default constructor

public List( )

{

this( DEFAULT_COLLECTION_SIZE );

}// end default constructor

// Parameterized constructor

public List(int initialSize )

{

theCollection =new Listable[initialSize];

numberOfElements = 0;

}// end parameterized constructor

publicvoid iterator(){

myIterator = 0;

return;

}

publicboolean hasNext(){

return myIterator < numberOfElements;

}

public Listable next(){

return theCollection[myIterator++];

}

publicvoid insert(Listable anElement){

// More logic is needed in this method.

// For example, is data structure (array) full?

// If so, what shall we do?

// If not, we insert the new element

// and increment the number of elements currently in the data structure by 1.

theCollection[numberOfElements++] = anElement;

return;

}// end of insert

/*

* Description: This method returns the desired element without deleting it

* from the collection of elements

*/

public Listable retrieve(Listable thisElement){

Listable theElement =null;

// verify precondition

if (numberOfElements <= 0){

// Perhaps it would be better if an exception was thrown in this situation.

System.out.println("\nThere are no elements!");

}else{

// Search for the element. When found, display it and stop searching.

for (int index = 0; index < numberOfElements; index++)

if ( ((Listable)theCollection[index]).compareTo(thisElement) == 0 ){

System.out.println("\nElement found!\n" + theCollection[index].toString());

// Get a handle to the element we are searching for.

// Or, do we want a copy of the element instead?

theElement = theCollection[index];

break;

}

}

// What if element number is not found?

// Perhaps it would be better if an exception was thrown in this situation.

return theElement;

}// end of retrieve

publicvoid delete(Listable thisElement){

// verify precondition

if (numberOfElements <= 0){

// Perhaps it would be better if an exception was thrown in this situation.

System.out.println("\nThere are no elements!");

}else{

// Assume the data structure is unsorted.

// This is a valid assumption since there were no requirements

// requiring the elements to be kept in a certain sort order.

for (int index = 0; index < numberOfElements; index++){

// Any element in this collection implements the method "getID()"

if ( ((Listable)theCollection[index]).compareTo(thisElement) == 0){

System.out.println("\nElement found!\n" + theCollection[index].toString());

System.out.println("\nElement to be deleted is:\n" + theCollection[index]);

// overwrite (i.e. delete element) this element with the

// last element in data collection

theCollection[index] = theCollection[--numberOfElements];

// Does the above deletion scheme work if the element to be deleted

// is the last one in our data structure?

break;

}

}

}

// What if element number is not found?

// Perhaps it would be better if an exception was thrown in this situation.

return;

}// end of delete

publicint size( )

{

return numberOfElements;

}// end of size

public String toString( )

// Desciption: Method to output the element information - for testing purposes

// Postconditon: The string containing all the element information is returned.

{

String theString ="";

if ( numberOfElements <= 0 )

theString ="\nThere are no elements!";

else

for (int index = 0; index < numberOfElements; index++ )

theString +="\n" + theCollection[ index ].toString( );

return theString;

}// end of toString

}

//Version 2....iterator implemented as an inner class of our List ADT class

import java.util.Iterator;

publicclass List<T>{

// Inner class implementing Java interface "Iterator<T>"

privateclass ListIteratorimplements Iterator<T>{

privateint theIterator;

public ListIterator(){

theIterator = 0;

return;

}

// Methods implemented from the Java Interface "Iterator<T>"

// Description: Returns true if the iteration has more elements.

// (In other words, returns true if next would return an

//element rather than throwing an exception.)

// Postcondition: Returns true if the iterator has more elements.

publicboolean hasNext(){

return theIterator < numberOfElements;

}

// Description: Returns the next element in the iteration.

// Postcondition: Returns the next element in the iteration.

// Throws: NoSuchElementException - iteration has no more elements.

public T next(){

return theCollection[theIterator++];

// Throwing an exception - to be implemented

}

// Description: Removes from the underlying collection the last element

// returned by the iterator (optional operation).

// This method can be called only once per call to next.

// The behavior of an iterator is unspecified if the underlying

// collection is modified while the iteration is in progress

// in any way other than by calling this method.

// Throws: UnsupportedOperationException - if the remove operation is not

// supported by this Iterator.

// IllegalStateException - if the next method has not yet been called,

// or the remove method has already been called after the last call

// to the next method.

publicvoid remove(){

// To be implemented

return;

}

}

//////////////////////////////////////////

finalstaticint DEFAULT_COLLECTION_SIZE = 25;

private T[] theCollection;// collection of elements that have an ID

// and that implements "getID()" of the IDable interface

privateint numberOfElements;// current number of elements

// Default constructor

public List( )

{

this( DEFAULT_COLLECTION_SIZE );

}// end default constructor

// Parameterized constructor

public List(int initialSize )

{

theCollection = (T[])new Object[initialSize];

numberOfElements = 0;

}// end parameterized constructor

public Iterator<T> getIterator(){

return ((Iterator<T>)new ListIterator());

}

publicvoid insert(T anElement){

// More logic is needed in this method.

// For example, is data structure (array) full?

// If so, what shall we do?

// If not, we insert the new element

// and increment the number of elements currently in the data structure by 1.

theCollection[numberOfElements++] = anElement;

return;

}// end of insert

/*

* Description: This method returns the desired element without deleting it

* from the collection of elements

*/

public T retrieve(T thisElement){

T theElement =null;

// verify precondition

if (numberOfElements <= 0){

// Perhaps it would be better if an exception was thrown in this situation.

System.out.println("\nThere are no elements!");

}else{

// Search for the element. When found, display it and stop searching.

for (int index = 0; index < numberOfElements; index++)

if ( ((Listable)theCollection[index]).compareTo((Listable)thisElement) == 0 ){

System.out.println("\nElement found!\n" + theCollection[index].toString());

// Get a handle to the element we are searching for.

// Or, do we want a copy of the element instead?

theElement = theCollection[index];

break;

}

}

// What if element number is not found?

// Perhaps it would be better if an exception was thrown in this situation.

return theElement;

}// end of retrieve

publicvoid delete(T thisElement){

// verify precondition

if (numberOfElements <= 0){

// Perhaps it would be better if an exception was thrown in this situation.

System.out.println("\nThere are no elements!");

}else{

// Assume the data structure is unsorted.

// This is a valid assumption since there were no requirements

// requiring the elements to be kept in a certain sort order.

for (int index = 0; index < numberOfElements; index++){

// Any element in this collection implements the method "getID()"

if ( ((Listable)theCollection[index]).compareTo((Listable)thisElement) == 0){

System.out.println("\nElement found!\n" + theCollection[index].toString());

System.out.println("\nElement to be deleted is:\n" + theCollection[index]);

// overwrite (i.e. delete element) this element with the

// last element in data collection

theCollection[index] = theCollection[--numberOfElements];

// Does the above deletion scheme work if the element to be deleted

// is the last one in our data structure?

break;

}

}

}

// What if element number is not found?

// Perhaps it would be better if an exception was thrown in this situation.

return;

}// end of delete

publicint size( )

{

return numberOfElements;

}// end of size

public String toString( )

// Desciption: Method to output the element information - for testing purposes

// Postconditon: The string containing all the element information is returned.

{

String theString ="";

if ( numberOfElements <= 0 )

theString ="\nThere are no elements!";

else

for (int index = 0; index < numberOfElements; index++ )

theString +="\n" + theCollection[ index ].toString( );

return theString;

}// end of toString

}

[20294 byte] By [Ivan1238a] at [2007-11-27 9:05:43]
# 1
Which two types would that be, and did you read the API docs of theirs? Or what's the difference between them? And "better" for what?(Sorry I'm not going to read all that code.)
CeciNEstPasUnProgrammeura at 2007-7-12 21:40:17 > top of Java-index,Java Essentials,Java Programming...
# 2
sorry for my unclear speclication....my two type is refering to version 1 and version2..
Ivan1238a at 2007-7-12 21:40:17 > top of Java-index,Java Essentials,Java Programming...
# 3

For Ivan1238:

In the version 1:

the code:

public void iterator() {

myIterator = 0;

return;

}

Although you implement your iterator, but it is not same as the Iterator of java.

In jdk, by the iterator , the mode which visits the Collection is unitive, it is not for a class to visit expeditous

Stone.lia at 2007-7-12 21:40:17 > top of Java-index,Java Essentials,Java Programming...