java arraylist last index code example

Example 1: ArrayList lastIndexOf(Object o) method in java

import java.util.ArrayList;
public class ArrayListLastIndexOfMethodExample
{
   public static void main(String[] args)
   {
      ArrayList<Integer> al = new ArrayList<Integer>();
      al.add(40);
      al.add(98);
      al.add(20);
      al.add(98);
      al.add(35);
      al.add(45);
      al.add(35);
      System.out.println("Last occurrence of element 98 is: " + al.lastIndexOf(98));
      System.out.println("Last occurrence of element 35 is: " + al.lastIndexOf(35));
   }
}

Example 2: arraylist get last

E e = list.get(list.size() - 1);

Tags:

Java Example