How to search for the last occurrence of an element in ArrayList with Java

1 Answer

0 votes
package javaapplication1;

import java.util.ArrayList;

public class JavaApplication1 {

    public static void main(String[] args) {

        ArrayList arrList = new ArrayList();
   
        arrList.add("Java");
        arrList.add("C#");
        arrList.add("C++");
        arrList.add("PHP");
        arrList.add("JavaScript");
        arrList.add("C");
        arrList.add("PHP");
        arrList.add("Swift");
        
        int index = arrList.lastIndexOf("PHP");
        if (index == -1)
            System.out.println("Not Found");
        else
            System.out.println("Found at last index: " + index);
  }
}
  
/*
run:

Found at last index: 6

*/

 



answered Sep 22, 2016 by avibootz
...