Close

Java Collections - ArrayList.lastIndexOf() Examples

[Last Updated: Dec 10, 2025]

Java Collections Java Java API 


Class:

java.util.ArrayList

java.lang.Objectjava.lang.Objectjava.util.AbstractCollectionjava.util.AbstractCollectionjava.util.CollectionCollectionjava.util.AbstractListjava.util.AbstractListjava.util.ListListjava.util.ArrayListjava.util.ArrayListjava.util.ListListjava.util.RandomAccessRandomAccessjava.lang.CloneableCloneablejava.io.SerializableSerializableLogicBig

Methods:

public int lastIndexOf (Object o)

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the highest index i such that Objects.equals(o, get(i)), or -1 if there is no such index.




Examples


package com.logicbig.example.arraylist;

import java.util.*;

public class LastIndexOfExample {

public static void main(String... args) {
// Find last occurrence index
ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "B", "C", "B", "A"));
int lastA = list.lastIndexOf("A");
int lastB = list.lastIndexOf("B");
System.out.println("Last A at: " + lastA);
System.out.println("Last B at: " + lastB);
}
}

Output

Last A at: 4
Last B at: 3
JDK 25




See Also