Home  >  Article  >  Java  >  Use java's ArrayList.indexOf() function to get the index position of the specified element

Use java's ArrayList.indexOf() function to get the index position of the specified element

PHPz
PHPzOriginal
2023-07-26 20:37:211681browse

Use java's ArrayList.indexOf() function to obtain the index position of a specified element

In Java programming, ArrayList is a commonly used collection class used to store and operate a set of objects. ArrayList provides many convenient methods to handle elements in the collection, one of which is the indexOf() function. The indexOf() function is used to obtain the index position of the specified element in the ArrayList.

The syntax of the indexOf() function is as follows:

public int indexOf(Object o)

The parameter o represents the element to be searched. This function searches from the beginning of the ArrayList, finds the first matching element and returns its index position. If the element does not exist in the ArrayList, -1 is returned.

The following is a sample code using the indexOf() function:

import java.util.ArrayList;

public class IndexOfExample {
    public static void main(String[] args) {
        // 创建一个ArrayList并添加一些元素
        ArrayList<String> fruits = new ArrayList<>();
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("orange");
        fruits.add("grape");
        fruits.add("watermelon");

        // 查找指定元素的索引位置
        int index = fruits.indexOf("orange");
        System.out.println("orange的索引位置是:" + index);

        index = fruits.indexOf("mango");
        System.out.println("mango的索引位置是:" + index);
    }
}

In the above code, an ArrayList object fruits is first created and some fruit elements are added. Then, use the indexOf() function to find the index positions of the two elements "orange" and "mango". Finally, print the results to the console.

Run the above code, the output is as follows:

orange的索引位置是:2
mango的索引位置是:-1

As you can see, for the element "orange" that exists in the ArrayList, the indexOf() function returns its index position 2. For the element "mango" that does not exist in the ArrayList, the indexOf() function returns -1.

In addition to strings, the indexOf() function can also search for other types of objects, such as integers, custom objects, etc. Just make sure that the object to be searched is consistent with the element type in the ArrayList.

Summary:

The index position of the specified element in the ArrayList can be easily obtained through the ArrayList.indexOf() function. In actual programming, some element operations can be performed based on the returned index position, such as deletion, replacement, etc. Please note that the indexOf() function only returns the first matching index position. If you need to get all matching index positions, you can use a loop or other methods to process it.

The above is the detailed content of Use java's ArrayList.indexOf() function to get the index position of the specified element. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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