in java using for each how to check previous and next elements. Is there any possibility to check previous and next element in for each loop
-
1sure. Post your code – user2342558 Oct 16 '19 at 12:19
-
1it is better to opt for old-school `for loop` with index – Kishita Variya Oct 16 '19 at 12:20
-
1You can store the previous element in a variable. But the next element will only be known at the next iteration. Why not use a standard for loop? – assylias Oct 16 '19 at 12:20
-
2You can also use listIterator becuase it has both next and previous method. – Amit Oct 16 '19 at 12:21
-
1if you like any answer you can surely upvote it.. but please don't downvote an answer unless it's totally wrong. Thank you :) Cheers! – Kishita Variya Oct 16 '19 at 12:31
4 Answers
No.
The "for each" construct uses Iterator
underneath, which only has a next()
and hasNext()
functions. No way to get previous()
.
Lists
have a ListIterator
which does allow looking at the previous element, but "for each" doesn't know how to use it. The only solution is therefore to remember the previous element in a separate variable, or just use a simple counting loop like this: for(int i=0;i< foo.length();i++)
.

- 28,965
- 9
- 65
- 105
Using "for each" only has next() and hasNext() methods, so it doesn't provides methods for reverse traversing or extracting elements.
Considering you have a ArrayList of Strings, You can use java.util.ListIterator
which provides methods such as hasPrevious()
and previous()
Below is the sample of how it can be used . ** Read the comments in side of code as it contains important details for using these methods.**
ArrayList<String> mylist = new ArrayList<>();
ListIterator<String> myListIterator = mylist.listIterator();
myListIterator.hasNext(); // Returns: true if list has next element
myListIterator.next(); // Returns: next element , Throws:NoSuchElementException - if the iteration has no next element
myListIterator.hasPrevious(); // Returns: true if list has previous element
myListIterator.previous(); //Returns: the previous element in the list , Throws: NoSuchElementException - if the iteration has no previous element
Hope this helps.
PS : You should have posted the code which you have done so far, posting question on stackoverflow which does not contain anything to show your effort is really BAD.

- 386
- 3
- 5
- 20
What would be the purpose of this?
Instead of a foreach
you should probably use a for
loop and indexing
for(int i=0;i<lenght;i++) {
list[i-1].dostuff //previous
list[i].dostuff //current
list[i+1].dostuff //next item
}
and don't forget to check if the next item and previous exists.

- 544
- 4
- 13
-
You don't check if the elements you're about to retrieve exist (I know you mention checking if the next item exists, but still) and you don't use braces for the for loop. – Giovani Vercauteren Oct 16 '19 at 12:22
-
3Your answer would cause an array out of bounds on the first go through and is syntactically wrong – locus2k Oct 16 '19 at 12:23
-
yes I know it is not perfect code, I'm just visualizing how it can be done – Andreas Andersson Oct 16 '19 at 12:24
Some of the limitations of for Each loop.Can read the whole article for clear overview https://www.geeksforgeeks.org/for-each-loop-in-java/
Also,This can be one way:
public class Main
{
public static void main(String[] args) {
String[] arr={"a","b"};
String next="";
String current="";
String prev="";
for(int i=0;i<arr.length;i++){
//previousElement
if(i>0){
prev=arr[i-1] ;
System.out.println("previous: "+prev);
}else{
prev="none";
System.out.println("previous: "+prev);
}
//currentElement
current=arr[i];
System.out.println(" current: "+current);
//nextElement
if(i<arr.length-1){
next=arr[i+1];
System.out.println(" next: "+next);
}else{
next="none";
System.out.println(" next: "+next);
}
}
}
}
P.S. can check the code on https://www.onlinegdb.com/online_java_compiler

- 680
- 7
- 18
-
2
-
@manikantgautam updated the solution. If satisfied kindly accept it – Ayushi Keshri Oct 16 '19 at 13:19