3) An Iterator (that is an object that has the methods in the Iterator interface
ID: 3852218 • Letter: 3
Question
3) An Iterator (that is an object that has the methods in the Iterator interface) allows access to each data element in a collection. How is that different from simply concatenating the values of the fields of all the data elements into a long string using the toString( ) method for a collection such as an OrderedList or UnorderedList?
public class ArrayIterator<T> implements Iterator<T> { private T arrayOfData; //this will hold the data from the collection to iterate over private int currentCusrsor; //this will hold the subscript of the next item to access
public ArrayIterator(T [ ] array, int numberOfElements) { //create an array to hold a copy of the collection to iterate over arrayOfData = (T[ ]) (new Object[numberOfElements]); for(int k = 0; k < arrayOfData.length; k++) { arrayOfData[k] = array[k]; } currentCursor = 0; }
public T next( ) { //if there is not another data item that has not been accessed yet //throw an exeption. I just made up this exception class if(!hasNext( )) throw new NothingLeftToIterateOverException(“In ArrayIterator”);
currentCursor ++; return [arrayOfData [currentCursor - 1]; }
pubic boolean hasNext( ) { //this method is supposed to return true if there are any data items //in the array that have not yet been accessed. You are to write the //one line of code to do this
}
public void remove( ) throws UnsupportedOperationExeption { //Because we are never going to use this method we will //simply create and throw and exception whenever this //method is called. This is not a Java built-in Exception class, //it is one the authors of the textbook created. throw new UnsupportedOperationException(“The remove operation for this iterator is not “ + supported”); } }//end of ArrayIterator class definition
6) Now write the class definition for the NothingLeftToIteratorOver exception used above.
Explanation / Answer
/**
*
* @author Sam
*/
public class ArrayIterator<T> implements Iterator<T> {
private T[] arrayOfData; //this will hold the data from the collection to iterate over
private int currentCursor; //this will hold the subscript of the next item to access
public ArrayIterator(T [ ] array, int numberOfElements) { //create an array to hold a copy of the collection to iterate over
arrayOfData = (T[ ]) (new Object[numberOfElements]);
for(int k = 0; k < arrayOfData.length; k++) {
arrayOfData[k] = array[k];
}
currentCursor = 0;
}
@Override
public T next() {
//if there is not another data item that has not been accessed yet
//throw an exeption. I just made up this exception class
if(!hasNext( ))
throw new NothingLeftToIterateOverException("In ArrayIterator");
currentCursor ++;
return arrayOfData [currentCursor - 1];
}
@Override
public boolean hasNext( ) {
//this method is supposed to return true if there are any data items
//in the array that have not yet been accessed. You are to write the
//one line of code to do this
return currentCursor != arrayOfData.length;
}
@Override
public void remove( ) throws UnsupportedOperationException {
//Because we are never going to use this method we will
//simply create and throw and exception whenever this
//method is called. This is not a Java built-in Exception class,
//it is one the authors of the textbook created.
throw new UnsupportedOperationException("The remove operation for this iterator is not " +
"supported");
}
}//end of ArrayIterator class definition
class NothingLeftToIterateOverException extends RuntimeException{
public NothingLeftToIterateOverException(String message) {
super(message);
}
}
Here you go champ... Like it?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.