for (String msg : messages) {
System.out.println(msg);
}
Say that messages is a properly initialized circular, doubly linked list that has no head or end pointer (reference). That is, it just has a current node pointer. My question simply is what exactly is the enhanced for loop doing for each iteration. If necessary, I have an interface that I can post for this special type of ADT that messages is.
Here is the iterator:
import java.util.*;
public class LinkedCircularSequenceIterator<E> implements Iterator<E> {
private DblListnode<E> curr;
private int itemsLeft;
public LinkedCircularSequenceIterator(DblListnode<E> curr, int numItems) {
this.curr = curr;
this.itemsLeft = numItems;
}
public boolean hasNext() {
return itemsLeft > 1;
}
public E next() {
if(!hasNext()) {
throw new NoSuchElementException();
}
curr = curr.getNext();
itemsLeft--;
return curr.getPrev().getData();
}
public void remove() {
throw new UnsupportedOperationException();
}
}
It should be doing something like
Iterator<Message> iter = messages.iterator();
while (iter.hasNext()){
msg=iter.next().toString();
System.out.println(msg);
}