ListmyList = new ArrayList<>(); myList.add("apple"); myList.add("banana"); myList.add("orange"); myList.add("apple"); int lastIndex = myList.lastIndexOf("apple"); //lastIndex will be 3
ListIn this example, we have created a List of type Integer using the asList() method which returns a fixed-size list backed by the specified array. Here the lastIndexOf(Integer) method is called with the Integer element 30 which returns the last index of 30 in the list which is 2. The lastIndexOf() method is a part of the java.util package.myList = Arrays.asList(10, 20, 30, 40); int lastIndex = myList.lastIndexOf(30); //lastIndex will be 2