Java How can I refer to the previous and next elements during iteration?

When I have a for loop, I use i to refer to the elements of my array, objects, etc.

How:
Current object: myArray[i]
Next Product: myArray[i+1]
Previous item: myArray[i-1]

But for now, I'm using a foreach loop ( for (Object elem : col) { ).
How can I refer to the previous item?
(I need to search in the "array" that I am doing with for (Object object : getComponents()) .

But when it returns true (so it finds what I'm looking for), it must execute the code of the previous and next elements.

Clarification: I have java.awt.Component elements!

+6
source share
4 answers
 JButton prev, next, curr; Component[] arr = getComponents(); for(int i=1;i<arr.length-1;i++) { if (yourcondition == true) { curr = (JButton) arr[i]; prev = (JButton) arr[i-1]; next = (JButton) arr[i+1]; } } 
+1
source

If the data structure is a list, you can use ListIterator directly. ListIterator is special because it contains both the next() and previous () methods

 List list = ...; ListIterator iter = list.listIterator(); //--only objects of type List has this while(iter.hasNext()){ next = iter.next(); if (iter.hasPrevious()) //--note the usage of hasPrevious() method prev = iter.previous(); //--note the usage of previous() method } 
+7
source

The foreach will not let you do this. My suggestion is to revert to using the old old Iterator style. for instance

 final Iterator itr=getComponents().iterator(); Object previous=itr.next(); Object current=itr.next(); while(itr.hasNext()){ Object next=itr.next(); //Do something with previous, current, and next. previous=current; current=next; } 
+5
source

Array indexing

If you have a data structure similar to an array (for example, an actual array or something like ArrayList ), then a link i , i-1 , i+1 will give good performance, so there is not much more. (Despite the fact that for every For-Each Loop loop, the For Loop index counter is not very funny and is one of the few caveats.)

The answer suggested by Sergey does something like this.

Generic ListIterator

If you can get a ListIterator (which is actually a pretty big guess), the answer suggested by Suraj might be enough. But note that next() and previous() move the position of the iterator . Therefore, if you did something like the following for each iteration of the loop: prev = previous(); current = next(); next = next(); previous() prev = previous(); current = next(); next = next(); previous() prev = previous(); current = next(); next = next(); previous() , you will complete approximately 4 iteration operations for each loop. This is not a big problem if the iteration is cheap , and, fortunately, this is often the case for data structures that offer a ListIterator .

Common decision

The general solution for any Iterable (or Iterator ) should not do random searches (as is possible with an array) or make assumptions regarding next() performance, which should be called no more than N times, where N is the number of available elements.

Here is one of these implementations:

 final Iterator<E> it = iterable.iterator(); for (E next = (it.hasNext() ? it.next() : null), current = null; next != null;) { E previous = current; current = next; next = it.hasNext() ? it.next() : null; // Do something using 'current', 'previous' and 'next'. // NB: 'previous' and/or 'next' are null when 'current' is // the first and/or last element respectively } 

Reason, this implementation has its reservations:

  • It will break if Iterable contains null elements.
  • Neither the current nor the next are valid, so they cannot be used directly in them fangled Java 8 lambdas.
0
source

Source: https://habr.com/ru/post/904097/


All Articles