Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 implements Iterator { 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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

4. Are my sources relevant?

Answered: 1 week ago