How can you loop backward? - Cracking the Java Coding Interview

preview_player
Показать описание
Cracking the #Java #Coding #Interview - Question 152: How can you loop backward?
Рекомендации по теме
Комментарии
Автор

In Java 21 you can also use the reversed() method on SequencedCollection and SequencedMap which returns a reversed view of the collection.

loic.bertrand
Автор

Technically all set/Maps can be iterated backwards too because they have a forwards iterating order that is consistent meaning you can easily implement a backwards implementation.

Speiger
Автор

Why predefined methodscwhile you can rotate using for loop starting from nth element by reducing 1 each until the index>=0. Also using do while starting ftom nth element by reducing 1 each until the index>=0

csm
Автор

If we have an ArrayDeque that has some elements, then we can use - descending iterator. something like this. It's super useful.
Deque<Integer> stack = new ArrayDeque<>();
Iterator<Integer> itr = stack.descendingItrerator();
while(itr.hasNext()) {
// Do some processing
someList.add(itr.next());
}

abhishekghosh
welcome to shbcf.ru