Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对ArrayList中Collection.contains方法的可疑调用_Java_Arraylist_Contains - Fatal编程技术网

Java 对ArrayList中Collection.contains方法的可疑调用

Java 对ArrayList中Collection.contains方法的可疑调用,java,arraylist,contains,Java,Arraylist,Contains,我收到一条警告,watchStore.contains是对java.util.Collection\contains的可疑调用。我怎样才能修好它?我想使用contains()查找具有匹配序列号的特定对象 public Watch findWatchBySerialNumber(long srch) { long s = srch; Watch watch = null; for(int i = 0; i < watchStore.size(); i++) {

我收到一条警告,
watchStore.contains
是对
java.util.Collection\contains
的可疑调用。我怎样才能修好它?我想使用
contains()
查找具有匹配序列号的特定对象

public Watch findWatchBySerialNumber(long srch) {
    long s = srch;
    Watch watch = null;

    for(int i = 0; i < watchStore.size(); i++) {
        watch = watchStore.get(i);
            if(watchStore.contains(s)) {
                System.out.print("item found");
                return watch;
            }
    } 
    System.out.print("item not found");
    return null; // watch is not found.
}
公共监视findWatchBySerialNumber(长srch){
长s=srch;
Watch=null;
对于(int i=0;i
假定
Watch
是类,
watchStore
是一个
列表
,并且
Watch
上存在一个字段
serialNo

public Optional<Watch> findWatchBySerialNumber(long serial) {
    return watchStore.stream()
                     .filter(w -> w.getSerialNo() == serial)
                     .findFirst();
}

您的
contains
不起作用,因为您的列表不包含
Long
s,它包含
Watch
s。这也是编译器认为它可疑的原因
包含
接受一个
对象
,但如果您要查找的对象对于列表中的内容没有可比的
等于
,则它将返回
false


在这种情况下,您必须遍历整个集合才能找到它,特别是因为您正在查找这些对象上的特定属性,而不是一个特定的、易于提供的值。

假定
Watch
是类,
watchStore
是一个
列表,并且
serialNo
字段存在于
Watch

public Optional<Watch> findWatchBySerialNumber(long serial) {
    return watchStore.stream()
                     .filter(w -> w.getSerialNo() == serial)
                     .findFirst();
}

您的
contains
不起作用,因为您的列表不包含
Long
s,它包含
Watch
s。这也是编译器认为它可疑的原因
包含
接受一个
对象
,但如果您要查找的对象对于列表中的内容没有可比的
等于
,则它将返回
false

在这种情况下,您必须遍历整个集合才能找到它,特别是因为您在这些对象上查找的是特定的属性,而不是特定的、易于提供的值

请问我怎样才能解决这个问题。我想使用contain()来查找 具有匹配序列号的特定对象

在这种情况下,重写Watch的equals()以使用serialNumber字段进行比较

然后添加接受serialNumber的构造函数

public class Watch {
    private final long serialNumber;

    public Watch(long serialNumber) {
        this.serialNumber = serialNumber;
    }

    @Override
    public boolean equals(Object obj) {
        return obj == this ||
            (obj instanceof Watch && ((Watch)obj).serialNumber == serialNumber);
    }

    @Override
    public int hashCode() {
        return (int)serialNumber;
    }
}
if(watchStore.contains(s)){
替换
if(watchStore.contains(watchToFind)){
其中
watchToFind=新手表;

我想用contain()找到一个 具有匹配序列号的特定对象

在这种情况下,重写Watch的equals()以使用serialNumber字段进行比较

然后添加接受serialNumber的构造函数

public class Watch {
    private final long serialNumber;

    public Watch(long serialNumber) {
        this.serialNumber = serialNumber;
    }

    @Override
    public boolean equals(Object obj) {
        return obj == this ||
            (obj instanceof Watch && ((Watch)obj).serialNumber == serialNumber);
    }

    @Override
    public int hashCode() {
        return (int)serialNumber;
    }
}

if(watchStore.contains(s)){
替换
if(watchStore.contains(watchToFind)){
其中
watchToFind=newwatch(s);

您可以使用org.apache.commons.lang.ArrayUtils包中的contains方法

检查该值是否在给定数组中

如果传入空数组,则该方法返回false

参数: 数组要搜索的数组 值查找要查找的值 返回: 如果数组包含对象,则为true

long[]imageHashes={12l,13l,14l,15l};
System.out.println(ArrayUtils.contains(imageHashes,13l));

您可以使用org.apache.commons.lang.ArrayUtils包中的contains方法

检查该值是否在给定数组中

如果传入空数组,则该方法返回false

参数: 数组要搜索的数组 值查找要查找的值 返回: 如果数组包含对象,则为true

long[]imageHashes={12l,13l,14l,15l};

System.out.println(ArrayUtils.contains(imageHashes,13l))

您可以添加所获得错误的堆栈跟踪吗您获得的确切错误是什么?什么是
watchStore
?什么是watchStore集合?Watch还是long?watchStore
从何而来?您可以添加所获得错误的堆栈跟踪吗?您获得的确切错误是什么?什么是
watchStore
?watchStore集合包含什么?Watch还是long?watchStore来自哪里?嗨,Makoto,如果找不到对象,Java 8示例中的代码返回什么?我猜也是null?不,它返回一个
Optional
的实例。您必须调用Optional来获取值,或者采取一些默认操作。代码已编译,但未返回包含该“序列号”的对象。因为我尝试打印该对象,但未打印任何内容。为什么?@Nabstar:您确定已覆盖该对象的默认
toString()
方法吗?是的,已覆盖该方法…但我未使用它打印具有匹配项的对象“序列号”。当
查找WatchBySerialNumber(长序列号)时,我只需使用
System.out.print()
打印它
返回对象。嗨,Makoto,如果找不到对象,Java 8示例中的代码会返回什么?我猜也是空的?不,它返回一个
可选的实例。
。您必须调用可选来获取值,或者采取一些默认操作。代码已编译,但不返回包含该值的对象”“序列号”。因为我试图打印出对象,但没有打印。为什么?@Nabstar:你确定你已经覆盖了该对象的默认
toString()
方法吗?是的,它被覆盖了…但我没有使用它打印出具有匹配“序列号”的对象。我只是使用
System.out.print()
findWatchBySerialNumber(长序列)
返回对象时打印它。您正在创建一个新的i