How can i iterate a ArrayDeque in reverse?
I would normally do this for an array but it's not indexable.
for (int i = delegate.size() - 1; i >= 0; i--) {}
thanks
util. ArrayDeque. descendingIterator() method is used to return an iterator of the same elements as the ArrayDeque but in the reverse order.
We can iterate the list in reverse order in two ways: Using List. listIterator() and Using for loop method.
reverse() method is a java. util. Collections class method. It reverses the order of elements in a list passed as an argument.
An ArrayDeque (also known as an “Array Double Ended Queue”, pronounced as “ArrayDeck”) is a special kind of a growable array that allows us to add or remove an element from both sides. An ArrayDeque implementation can be used as a Stack (Last-In-First-Out) or a Queue(First-In-First-Out).
You can use the method descendingIterator()
to get an iterator that iterates the ArrayDeque<T>
in reverse.
Iterator<T> it = arrayDeque.descendingIterator();
while(it.hasNext()) {
// do something with it.next()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With