Java ArrayList contains 方法如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18920622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:31:08  来源:igfitidea点击:

How ArrayList contains method work?

javaobjectarraylistequalsprimitive

提问by AlessioG

I've a doubt about how ArrayList contains method works. Let's take an example:

我对 ArrayList contains 方法的工作方式有疑问。让我们举个例子:

List<String> lstStr = new ArrayList<String>();
String tempStr1 = new String("1");
String tempStr2 = new String("1");

lstStr.add(tempStr1);

if (lst.contains(tempStr2))
    System.out.println("contains");
else
    System.out.println("not contains");

it returns 'not contains'.

它返回“不包含”。

Another example:

另一个例子:

List<LinkProfileGeo> lst = new ArrayList<LinkProfileGeo>();
LinkProfileGeo temp1 = new LinkProfileGeo();
temp1.setGeoCode("1");
LinkProfileGeo temp2 = new LinkProfileGeo();
temp2.setGeoCode("1");

lst.add(temp1);

if (lst.contains(temp2))
    System.out.println("contains");
else
   System.out.println("not contains");

It returns contains. So how does contains method works ?

它返回包含。那么 contains 方法是如何工作的呢?

Thanks

谢谢

回答by Juned Ahsan

You are adding your string to the list lstStr

您正在将字符串添加到列表中 lstStr

lstStr.add(tempStr1);

but you are using containsmethod on lst

但你正在使用contains方法lst

if (lst.contains(tempStr2))

Your idea of testing is correct, as containsinternally uses equalsto find the element, so if the string is matched using equals then it should return true. But it seems you are using two different lists, one for adding and another one for checking contains.

您的测试想法是正确的,因为contains内部用于equals查找元素,因此如果使用 equals 匹配字符串,则它应该返回 true。但似乎您正在使用两个不同的列表,一个用于添加,另一个用于检查包含。

回答by Derek

the second part is a duplicate of: How does a ArrayList's contains() method evaluate objects?

第二部分是以下内容的副本:ArrayList 的 contains() 方法如何评估对象?

You need to override the equals method to make it work as you desire.

您需要覆盖 equals 方法以使其按您的意愿工作。

回答by jtravaglini

Here is the relevant source code from ArrayListif you're interested. As @user2777005 noted, you had a typo in your code. You should use lstStr.contains(), NOT lst.contains().

ArrayList如果您有兴趣,这里是相关的源代码。正如@user2777005 所指出的,您的代码中有一个错字。您应该使用lstStr.contains(),而不是lst.contains()

     public int indexOf(Object o) {
        if (o==null) {
            for (int i=0; i<a.length; i++)
                if (a[i]==null)
                    return i;
        } else {
            for (int i=0; i<a.length; i++)
                if (o.equals(a[i]))
                    return i;
        }
        return -1;
    }

    public boolean contains(Object o) {
        return indexOf(o) != -1;
    }

回答by Mukund Kumar

in first section of code :

在代码的第一部分:

String tempStr1 = new String("1");
String tempStr2 = new String("1");

both tempStr1and tempStr2refer two different-2 object of string. after that String object that is refered by tempStr1 is added to the List by the codelstStr.add(tempStr1);.so the List have only one String object that is reffered by tempStr1not tempStr2.but contains();method work on equals()method.that is lstStr.contains(temp2);return true if content of String object which is refered by temp2is same as the one of content of String object which is added to the Listand return false when matching not found.here lstStr.contains(temp2);return true because content of String object temp2 is equal to content of String object temp1 which is added to List.but in your code insteed of lstStr.contains(temp2);it is mentioned as:

两者tempStr1tempStr2引用字符串的两个不同的 2 对象。之后由tempStr1 refered String对象被添加到列表codelstStr.add(tempStr1);。所以列表有由下文称只有一个String对象tempStr1不是tempStr2。但 contains();在方法工作equals()method.that是 lstStr.contains(temp2);,如果String对象的内容通过refered回归真实temp2与添加到 的 String 对象的内容相同,List并在匹配未找到时返回 false。此处lstStr.contains(temp2);返回 true,因为 String 对象 temp2 的内容等于添加到List.but 在您的代码中的 String 对象 temp1 的内容中lstStr.contains(temp2);它被提及为:

lst.contains(temp2);

Here you are using different Listreference variable (lst) instead of (lstStr).thats why it return false and executed else part.

在这里,您使用了不同的List引用变量 ( lst) 而不是 ( lstStr)。这就是为什么它返回 false 并执行 else 部分的原因。

in 2nd section of code setGeoCode()is not defined.

在代码的第二部分中setGeoCode()没有定义。