📜  Java中的 Arraylist lastIndexOf() 示例

📅  最后修改于: 2022-05-13 01:55:32.401000             🧑  作者: Mango

Java中的 Arraylist lastIndexOf() 示例

Java中ArrayListlastIndexOf()方法用于获取 ArrayList 对象中元素最后一次出现的索引。

句法 :

lastIndexOf(element)

参数:要返回其最后一个索引的元素。

返回:
它返回参数中传递的元素的最后一次出现。如果未找到该元素,则返回 -1。

演示 lastIndexOf() 工作的程序:

// Java code to demonstrate the working of
// lastIndexOf() method in ArrayList
  
// for ArrayList functions
import java.util.ArrayList;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // creating an Empty Integer ArrayList
        ArrayList arr = new ArrayList(7);
  
        // using add() to initialize values
        arr.add(10);
        arr.add(20);
        arr.add(30);
        arr.add(40);
        arr.add(30);
        arr.add(30);
        arr.add(40);
  
        System.out.println("The list initially " + arr);
  
        // last index of 30
  
        int element = arr.lastIndexOf(30);
        if (element != -1)
            System.out.println("the lastIndexof of" + 
                             " 30 is " + element);
        else
            System.out.println("30 is not present in" + 
                                        " the list");
  
        // last index of 100
        element = arr.lastIndexOf(100);
        if (element != -1)
            System.out.println("the lastIndexof of 100" + 
                                      " is " + element);
        else
            System.out.println("100 is not present in" + 
                                           " the list");
    }
}

输出 :

The list initially [10, 20, 30, 40, 30, 30, 40]
the lastIndexof of 30 is 5
100 is not present in the list