Using Iterators!
Recall, we created an OrderedArray class (in class) where the base data
type of the ordered array was an object. In our case, it was a class
called Student:
public class Student
{
private String LastName;
private int credits;
private float gpa;
public Student(String s, int i, float f)
{
LastName=new String(s);
credits=i;
gpa=f;
}
public String toString()
{
return(LastName+" "+credits+" "+gpa);
}
public int compareTo(Student otherStudent)
{
return(LastName.compareTo(otherStudent.LastName));
}
}
The OrderedArray class was an array of objects of type Student. To
print out the array, we included a method called printIt() that printed
out the entire array. But suppose we wanted to do the following: we
wish to retrieve an element from the array, do something with it, and
then retrieve the next element in the array. Why (or when) you might
wish to do this will become apparent as you work on the programming
assignment.
We need an iterator.
An iterator is an element that is part of the OrderedArray object; it
is a current pointer to an element in the array. We can initialize this
pointer to the beginning of the array or the end. We can advance the
pointer forward or backward.
Finally, and most importantly, we can retrieve the element in array
referenced by the iterator. The declaration of an iterator in our
example would be:
public class OrderedArray
{
private Student data[ ];
private int nextElem;
private int maxSize;
private int theIterator; //we add an iterator;
.......
We can initialize the iterator to reference the beginning of the array
or the end of the array. We agree that if the array is empty, either
initialization should set the iterator to -1:
public void setIteratorBegin()
{
theIterator=(nextElem>0? 0 : -1);
}
public void setIteratorEnd()
{
theIterator=(nextElem>0 ? nextElem-1 : -1);
}
Finally, there are three methods that return an instance of a Student:
/* getIterator returns a reference to the Student currently referenced by theIterator
/* if theIterator==1 return -1
public Student getIterator()
{
return(theIterator==-1 ? null :data[theIterator]);
}
/* advance theIterator. If we run off end, set to -1
/* if theIterator==-1 return null, else the reference to the entry in the array
public Student getIteratorNext()
{
theIterator=(theIterator==nextElem-1? -1: theIterator+1);
return(theIterator==-1? null : data[theIterator]);
}
/* decrement theIterator. If we run off the beginning of the array, set to -1
/* if theIterator==-1 return null, else the reference to the entry in the array
public Student getIteratorPrev()
{
theIterator=(theIterator==0? -1: theIterator-1);
return (theIterator==-1?null:data[theIterator]);
}
So what does all this get us ? See the following code to traverse the array foward or in reverse:
public class UseOrderedArray {
public static void main(String[] args)
{
Student s;
OrderedArray myArray=new OrderedArray(20);
myArray.insert(new Student("smith",16,(float)2.7));
myArray.insert(new Student("adams",15,(float)3.1));
myArray.insert(new Student("morris",17,(float)3.3));
// print in reverse order
myArray.setIteratorEnd();
for(s=myArray.getIterator(); s!=null;s=myArray.getIteratorPrev())
System.out.println(s);
// print in forward order
myArray.setIteratorBegin();
for(s=myArray.getIterator(); s!=null;s=myArray.getIteratorNext())
System.out.println(s);
}
}