1

I have class which has an Iterator method ( public Iterator iterator(boolean fromFront) ) and my Iterator method returns a "new DoublyLinkedListIterator()". DoublyLinkedListIterator implements ListIterator which contains the set method. The problem is in my main where my iterator doesn't have access to the set method, Iterator iterator = deque.iterator(true); Nor do I have access to the hasPrevious or previous methods.

3 Answers3

1

If you want to have access to the methods in DoublyLinkedListIterator, you have to return it instead of an Iterator. (Or make a cast before using it).

How to make a cast, in case you need it.

Community
  • 1
  • 1
dquijada
  • 1,697
  • 3
  • 14
  • 19
0

The standard way to expose a ListIterator is to have a public <T> ListIterator<T> listIterator() method on your class, as in the standard List class, so you can use it as:

ListIterator literator = deque.listIterator();

Note that the builtin Deque<T> class does not support ListIterators. The variable name is taken from your sample code, it has a custom type.

Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97
-2

If you want to use iterator but want to navigate forward/backward and change the links order, ... to be short every write operation that modify the structure of the list, you're doing it wrong.

Go for the while/do-while loop if you have such needs.

Walfrat
  • 5,363
  • 1
  • 16
  • 35