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/