Java.util.ArrayList.lastIndexOf() Method


Advertisements


Description

The java.util.ArrayList.lastIndexOf(Object) method returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

Declaration

Following is the declaration for java.util.ArrayList.lastIndexOf() method

public int lastIndexOf(Object o)

Parameters

o − The element to search for.

Return Value

This method returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

Exception

NA

Example

The following example shows the usage of java.util.ArrayList.lastIndexOf() method.

Live Demo
package com.tutorialspoint;

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {

      // create an empty array list with an initial capacity
      ArrayList<String> arrlist = new ArrayList<String>(5);

      // use add() method to add values in the list
      arrlist.add("G");
      arrlist.add("E");
      arrlist.add("F");
      arrlist.add("M");
      arrlist.add("E");
      
      System.out.println("Size of list: " + arrlist.size());

      // let us print all the values available in list
      for (String value : arrlist) {
         System.out.println("Value = " + value);
      }  

      // Retrieving the index of last occurrence of element "E"
      int retval = arrlist.lastIndexOf("E");
      System.out.println("The last occurrence of E is at index " + retval);
   }
}   

Let us compile and run the above program, this will produce the following result −

Size of list: 5
Value = G
Value = E
Value = F
Value = M
Value = E
The last occurrence of E is at index 4

java_util_arraylist.htm


Advertisements
E-Books Store