Hey guys I've been teaching myself java and I working on this assignment. http://ljing.org/games/focus/
So I write a Linked list from scratch, I write a Deque class using the LinkedList class But!
There's only one question I don't understand about the classes Iterator. I just don't understand what does the Class Deque Iterator is supposed to do.
Also, I have this in my code:
class Deque<Item> implements Iterable<Item>.
But then the compiler complains that in my Deque class I need to override a method
@Override
public Iterator<Item> iterator()
{
throw new UnsupportedOperationException("Not supported yet.");
}
But I don't understand why
An iterator is a concept for accessing the elements in a collection. Because you say implements Iterable<Item>
, you tell the compiler that you provide this mechanism of accessing the elements of your Deque. But that's not enough yet. Besides claiming you will do it, you actually have to do it. In this case, doing it is implementing the method.
What happens if you don't:
Because you told the compiler you would provide this, you have to implement the method iterator()
, which is part of this access concept. If you do not implement the method, the compiler complains and tells you "hey, you said you would do it. So keep your word!".
There are two ways to solve this:
1.) In the first place, don't give your word you would provide the access concept through an iterator - remove implements Iterable<Item>
2.) Keep your word and implement the method. You will have to write an own Iterator class for this. That's a rather short task once you know what to do.