Java ArrayList lastIndexOf() 方法



描述

Java ArrayList lastIndexOf(Object) 方法返回此列表中指定元素最後一次出現的索引,如果此列表不包含該元素,則返回 -1。

宣告

以下是 java.util.ArrayList.lastIndexOf() 方法的宣告

public int lastIndexOf(Object o)

引數

o − 要搜尋的元素。

返回值

此方法返回此列表中指定元素最後一次出現的索引,如果此列表不包含該元素,則返回 -1。

異常

在整數 ArrayList 中獲取元素的最後一個索引示例

以下示例演示了 Java ArrayList lastIndexOf(object) 方法的使用。我們正在建立一個整數 ArrayList。我們使用 add() 方法為每個元素新增幾個整數到 ArrayList 物件中,並使用 lastIndexOf(object) 方法檢查元素的最後一個索引並列印它。

package com.tutorialspoint;

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {
      
      // create an empty array list 
      ArrayList<Integer> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add(0);
      arrayList.add(1);
      arrayList.add(2);
      arrayList.add(5);
      arrayList.add(4);
      arrayList.add(5);
      arrayList.add(6);
	
      // let us print last index of 5
      System.out.println("Last index of 5 = " + arrayList.lastIndexOf(5));
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

Last index of 5 = 5

在字串 ArrayList 中獲取元素的最後一個索引示例

以下示例演示了 Java ArrayList lastIndexOf(object) 方法的使用。我們正在建立一個字串 ArrayList。我們使用 add() 方法為每個元素新增幾個字串到 ArrayList 物件中,並使用 lastIndexOf(object) 方法檢查元素的最後一個索引並列印它。

package com.tutorialspoint;

import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String[] args) {
      
      // create an empty array list
      ArrayList<String> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add("A");
      arrayList.add("B");
      arrayList.add("C");
      arrayList.add("B");
	  
      // let us print index of B
      System.out.println("Last index of B = " + arrayList.lastIndexOf("B"));     
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

Last index of B = 3

在物件 ArrayList 中獲取元素的最後一個索引示例

以下示例演示了 Java ArrayList lastIndexOf(object) 方法的使用。我們正在建立一個 Student 物件的 ArrayList。我們使用 add() 方法為每個元素新增幾個 Student 物件到 ArrayList 物件中,並使用 lastIndexOf(object) 方法檢查元素的最後一個索引並列印它。

package com.tutorialspoint;

import java.util.ArrayList;

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

      // create an empty arrayList
      ArrayList<Student> arrayList = new ArrayList<>();

      // use add() method to add elements in the arrayList
      arrayList.add(new Student(1, "Julie"));
      arrayList.add(new Student(2, "Robert"));
      arrayList.add(new Student(3, "Adam"));
      arrayList.add(new Student(4, "Adam"));

      // let us print last index of Adam
      System.out.println("Last index of Adam = " + arrayList.lastIndexOf(new Student(4, "Adam")));      
   }
}

class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

Last index of Adam = 3
java_util_arraylist.htm
廣告