Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i iterate a ArrayDeque in reverse?

Tags:

java

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

like image 877
CodingHero Avatar asked Sep 12 '13 09:09

CodingHero


People also ask

How do you reverse ArrayDeque?

util. ArrayDeque. descendingIterator() method is used to return an iterator of the same elements as the ArrayDeque but in the reverse order.

Can you iterate list forward and backward?

We can iterate the list in reverse order in two ways: Using List. listIterator() and Using for loop method.

How do you reverse a list in Java?

reverse() method is a java. util. Collections class method. It reverses the order of elements in a list passed as an argument.

Is ArrayDeque a queue or stack?

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).


1 Answers

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()
}
like image 128
RaptorDotCpp Avatar answered Sep 24 '22 21:09

RaptorDotCpp