-1

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

dileep .b
  • 47
  • 1
  • 7

4 Answers4

5

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

Malt
  • 28,965
  • 9
  • 65
  • 105
1

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.

prnjn
  • 386
  • 3
  • 5
  • 20
0

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.

0

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);
                              }
                }
  }
}

Attaching the output also: enter image description here

P.S. can check the code on https://www.onlinegdb.com/online_java_compiler

Ayushi Keshri
  • 680
  • 7
  • 18