El método lastIndexOf(Object obj)
devuelve el índice de la última aparición del elemento especificado en el archivo ArrayList
. Devuelve -1 si el elemento especificado no existe en la lista.
public int lastIndexOf(Object obj)
Esto devolvería el índice de la última aparición del elemento Obj en ArrayList.
Ejemplo
En el siguiente ejemplo, tenemos un Integer ArrayList que tiene algunos elementos duplicados. Estamos obteniendo el último índice de algunos elementos usando lastIndexof
método.
package beginnersbook.com; import java.util.ArrayList; public class LastIndexOfExample { public static void main(String args[]) { //ArrayList of Integer Type ArrayList<Integer> al = new ArrayList<Integer>(); al.add(1); al.add(88); al.add(9); al.add(17); al.add(17); al.add(9); al.add(17); al.add(91); al.add(27); al.add(1); al.add(17); System.out.println("Last occurrence of element 1: "+al.lastIndexOf(1)); System.out.println("Last occurrence of element 9: "+al.lastIndexOf(9)); System.out.println("Last occurrence of element 17: "+al.lastIndexOf(17)); System.out.println("Last occurrence of element 91: "+al.lastIndexOf(91)); System.out.println("Last occurrence of element 88: "+al.lastIndexOf(88)); } }
Producción:
Last occurrence of element 1: 9 Last occurrence of element 9: 5 Last occurrence of element 17: 10 Last occurrence of element 91: 7 Last occurrence of element 88: 1