Home > Java > Java Tutorial > body text

Java uses the contains() function of the ArrayList class to determine whether an element exists

WBOY
Release: 2023-07-24 19:33:15
Original
1114 people have browsed it

Java uses the contains() function of the ArrayList class to determine whether an element exists

In Java programming, ArrayList is a very commonly used data structure. It provides a flexible way to store and manipulate a set of data. In addition to simply adding, deleting and accessing elements, ArrayList also provides some useful methods, such as the contains() function, which is used to determine whether an element exists in the ArrayList.

contains() function is a member function of the ArrayList class. Its function is to determine whether the specified element exists in the ArrayList. This function returns a Boolean value that returns true if the element exists; false if the element does not exist. The syntax format of the contains() function is as follows:

public boolean contains(Object element)

When calling the contains() function, you need to pass in an Object type parameter to specify the element to be judged. . For elements of basic data types, wrapper classes can be used for packaging.

The following is a sample code that demonstrates how to use the contains() function to determine whether an element exists in an ArrayList:

import java.util.ArrayList;
import java.util.List;

public class ArrayListExample {

    public static void main(String[] args) {
        // 创建一个ArrayList对象
        List fruits = new ArrayList<>();

        // 添加一些水果到ArrayList中
        fruits.add("苹果");
        fruits.add("香蕉");
        fruits.add("橙子");
        fruits.add("葡萄");
        fruits.add("西瓜");

        // 判断是否存在指定的水果
        System.out.println(fruits.contains("苹果"));  // 输出:true
        System.out.println(fruits.contains("葡萄"));  // 输出:true
        System.out.println(fruits.contains("草莓"));  // 输出:false
    }
}
Copy after login

In the above code, we create an ArrayList object and Added some fruity elements to it. Then, we use the contains() function to determine whether certain fruits exist in the ArrayList. The result outputs the judgment result of each fruit.

In actual applications, the contains() function is very useful. It can be used to check whether an element already exists in the collection, thereby avoiding duplicate elements. For example, in a teacher management system, we can use ArrayList to store students' names, and use the contains() function to determine whether a student already exists in the student list to avoid repeated additions.

It should be noted that the contains() function uses the equals() method of the element to determine whether the elements are equal. Therefore, when using the contains() function in a custom class, you need to override the equals() method to ensure correct comparison.

In short, the ArrayList class in Java provides the contains() function to determine whether an element exists in the ArrayList. This function is very convenient and practical, and can help us better process collection data in programming. By rationally using the contains() function, we can improve the readability and maintainability of the code.

The above is the detailed content of Java uses the contains() function of the ArrayList class to determine whether an element exists. 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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!