java - 根据对象类型的数组列表搜索特定输入

标签 java arraylist

public void searchWatch(long srch){
    long s = srch;
    boolean found = false;

    for(int i = 0; i<watchStore.size();i++){
        Watch fd = watchStore.get(i);

        if(fd.equals(s)){
            System.out.print("item found");
            found = true;
        }

        if(!found){
            System.out.println("no such record");
        }
    } 
}

this is a code fragment from one of my class. my question here is I want to test a particular input of type long against an arraylist of type Watch. whether the serial number exist in the arraylist.

但由于错误“.equal() on incompile types”而失败,上面的代码有什么问题

以下是修改后的代码

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)){ // this pop an error called  suspicious call to java.utit.Collection.contains
                    System.out.print("item found");
                    return watch;
                }
        } 
    System.out.print("item not found");
    return null; // watch is not found.
}

please how can I fix that.

最佳答案

当您执行 if(fd.equals(s)){ 时,您正在尝试将 String 与另一个 Watch 类型的对象进行匹配这就是您收到错误的原因。

您需要获取fd的字符串表示形式,然后将其与s进行匹配。

关于java - 根据对象类型的数组列表搜索特定输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33111659/

相关文章:

Java Jpanel setBounds 不工作

java - 如何防止连续两次询问相同的乘法数字

java - 对孤立的 arraylist 元素执行乘法

Java快速数组列表

java - 如何使用for循环删除ArrayList中的数据

java - 嵌套数组列表

java - GlassFish 构建失败

java - JBOSS 部署警告 - 包含 CDI 注释但找不到 beans.xml

源代码 1.6 不支持 Java Combo Box Diamond Operator

JAVA更新ArrayList对象方法不更新