Java - The method descendingIterator() is undefined for the type List<Object>

Viewed 358

My code is

List<Object> listOld = new LinkedList<Object>();
listOld.add("Hello");
listOld.add(1000);
listOld.add(25);
listOld.add(85.9);
Iterator x = listOld.descendingIterator();

Error is:

Tester.java:10: error: cannot find symbol Iterator x = listOld.descendingIterator(); ^ symbol: method descendingIterator() location: variable listOld of type List 1 error

How can I solve this problem?

1 Answers

Method Iterator<E> descendingIterator() is declared in the interace java.util.Deque<E> extends Queue<E>. LinkedList is an implementation of Deque, and List interface does not extend Deque.

So if you change your code to the following, it should work.

LinkedList<Object> listOld = new LinkedList<Object>();
Iterator x = listOld.descendingIterator();
Related