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.
Asked
Active
Viewed 104 times
1
-
4Please post your code also. – js_248 Apr 18 '16 at 08:56
-
if you are sure that you're calling correct method, you can cast returned iterator to your specific DoublyLinkedListIterator – AdamSkywalker Apr 18 '16 at 08:57
-
Also, what is your question ? – Nico Apr 18 '16 at 08:57
-
set the return type to ListIterator instead of Iterator, or cast it in the main – Tamas Hegedus Apr 18 '16 at 09:05
3 Answers
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.
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 ListIterator
s. 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