search
HomeJavaJavagetting StartedDetailed example of contains method in ArrayList

Detailed example of contains method in ArrayList

Aug 24, 2020 pm 03:57 PM
arraylistcontains

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;
}

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;
}

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;
}

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;
}

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!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.