simple iterator and iterator implemented as an inner class
hi all...i got a problem when i am reading my java text book ..there are two version 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 version of iterator as follwoing?
Version 1: simple iterator for our List.
Version 2....iterator implemented as an inner class of our List ADT
//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( )
{
}// end default constructor
// Parameterized constructor
public List(int initialSize )
{
}// end parameterized constructor
publicvoid iterator(){
}
publicboolean hasNext(){
}
public Listable next(){
}
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.
}// end of insert
/*
* Description: This method returns the desired element without deleting it
* from the collection of elements
*/
public Listable retrieve(Listable thisElement){
}// end of retrieve
publicvoid delete(Listable thisElement){
}// 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.
}
}
======================
//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(){
}
// 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(){
}
// 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(){
// 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( )
{
}// end default constructor
// Parameterized constructor
public List(int initialSize )
{
}// end parameterized constructor
public Iterator<T> getIterator(){
}
publicvoid insert(T anElement){
}// end of insert
/*
* Description: This method returns the desired element without deleting it
* from the collection of elements
*/
public T retrieve(T thisElement){
// 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){
}
// 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( )
{
}// 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.
}
}
Message was edited by:
Ivan1238
Message was edited by:
Ivan1238
Message was edited by:
Ivan1238

