Home > Java > javaTutorial > Find last index of element in Java using lastIndexOf() method of ArrayList class

Find last index of element in Java using lastIndexOf() method of ArrayList class

PHPz
Release: 2023-07-25 10:24:24
Original
1188 people have browsed it

Use the lastIndexOf() method of the ArrayList class to find the last index of an element in Java

In Java, ArrayList is a commonly used collection class, which provides many convenient methods to operate and find elements. One of the very useful methods is lastIndexOf(), which can help us find the last index position of the element in the ArrayList.

The following is a simple code example that shows how to use the lastIndexOf() method of ArrayList to find the last index of an element.

import java.util.ArrayList;

public class LastIndexOfExample {
    public static void main(String[] args) {
        // 创建一个ArrayList对象并添加元素
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("苹果");
        fruits.add("香蕉");
        fruits.add("橙子");
        fruits.add("苹果");
        fruits.add("橙子");

        // 输出整个ArrayList
        System.out.println("ArrayList中的元素:" + fruits);

        // 查找元素的最后一个索引位置
        String target = "苹果";
        int lastIndex = fruits.lastIndexOf(target);

        // 判断是否找到元素
        if (lastIndex != -1) {
            System.out.println("元素 "" + target + "" 的最后一个索引位置是:" + lastIndex);
        } else {
            System.out.println("未找到元素 "" + target + """);
        }
    }
}
Copy after login

In the above code, we first create an ArrayList object named "fruits" and add some fruit elements. Then, we use the lastIndexOf() method to find the last index position of the element "apple" in the ArrayList. Finally, it is judged based on the result whether the element is found, and the corresponding information is output.

Run the above code and the output obtained is as follows:

ArrayList中的元素:[苹果, 香蕉, 橙子, 苹果, 橙子]
元素 "苹果" 的最后一个索引位置是:3
Copy after login

As can be seen from the output result, the last index position of the element "Apple" in the ArrayList is 3.

The above code example shows the basic usage of the lastIndexOf() method of the ArrayList class, but it also has some caveats. For example, the lastIndexOf() method searches the index of an element from back to front. If it contains duplicate elements, it returns the last matching index position. If you want to find the first matching index position, you can use the indexOf() method. In addition, if the ArrayList does not contain the element you are looking for, the lastIndexOf() method will return -1.

In short, the lastIndexOf() method of the ArrayList class is a convenient tool that can help us find the last index position of an element in Java. In actual development, we can flexibly use this method as needed to improve the efficiency and accuracy of the code.

The above is the detailed content of Find last index of element in Java using lastIndexOf() method of ArrayList class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template