Home > Java > Javagetting Started > body text

Detailed example of contains method in ArrayList

王林
Release: 2020-08-24 15:57:46
forward
4776 people have browsed it

Detailed example of contains method in ArrayList

The contains method in ArrayList is used to determine whether the target element is included in the ArrayList.

(Recommended tutorial: java introductory tutorial)

Principle:

Call the indexOf(Object o) method

public boolean contains(Object o) {
    return indexOf(o) >= 0;
}
Copy after login

indexOf( Object o) method calls the equals method of the incoming Object object for comparison

public int indexOf(Object o) {
    // 传入的Object是null, 则在集合中寻找为null的元素
    if (o == null) {
        for (int i = 0; i < size; i++)
            if (elementData[i]==null)
                return i;
    } else { // 如果不为null, 调用equals方法比较
        for (int i = 0; i < size; i++)
            if (o.equals(elementData[i]))
                return i;
    }
    // 不满足条件, 返回-1
    return -1;
}
Copy after login

Usage:

Now that the principle is clear, the next thing to do is to take a look at the equals method of common classes

(Learning video recommendation: java course)

String class

public boolean equals(Object anObject) {
    // 如果两个对象内存地址相同, 返回true
    if (this == anObject) {
        return true;
    }
    // 判断传入Object是String的情况
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            // 比较String转化的char[]中的每一个char元素
            // 如果有一个不想等,则返回false
            while (n-- != 0) {
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}
Copy after login

So, if the element type in the ArrayList collection is String, use contains directly The method is no problem

Integer class

Other packaging types are basically the same as it, they are all compared values, so you can also use the contains method directly

public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return value == ((Integer)obj).intValue();
    }
    return false;
}
Copy after login

Other reference types

I believe everyone knows that when using other reference types and need to use the contains method, you must override the equals method!

The above is the detailed content of Detailed example of contains method in ArrayList. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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