簡體   English   中英

根據對象類型的數組列表搜索特定輸入

[英]searching a particular input against an arraylist of object type

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

這是我班上一個的代碼片段。 我的問題是我想針對Watch類型的arraylist測試long類型的特定輸入。 序列號是否存在於arraylist中。

但由於錯誤“不兼容類型上的.equal()”而失敗,上述代碼有什么問題

以下是修改后的代碼

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

請我該如何解決。

當您執行if(fd.equals(s)){您正在嘗試將StringWatch類型的另一個對象進行匹配,這就是為什么您會收到錯誤消息。

您需要獲取fd的String表示形式,然后將其與s匹配。

更換:

Watch fd = watchStore.get(i);

帶有:

Watch fd = watchStore.get(i);`
// use getter method
String fdString = fd.getSerial();
if(fdString.equals(s)){
        System.out.print("item found");
        found = true;
    }

看看是否有幫助。

'fd'是Watch的對象,'s'是String的對象。 由於這是兩個不同的類,因此運行fd.equals(s)將引發錯誤。 要使其正常工作,請嘗試在Watch類中重寫toString()方法,然后執行

fd.toString().equals(s)

如果嘗試按序列號查找,則:

public void searchWatch (long srch){
    boolean isFound = false;
    Watch fd = null; // declaring variable out of the loop is better.
    for(int i = 0; i<watchStore.size();i++){
        fd = watchStore.get(i);

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

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

我的建議:如果將方法名稱寫為search或發現,則應返回一個對象。 如果只需要知道“它是否存在”,則可以為您的方法命名: isWatchExist()並添加一個布爾返回類型。

public boolean isWatchExist (long serialNumber) {
    Watch watch = null; // declaring variable out of the loop is better.
    for(int i = 0; i < watchStore.size(); i++){
        watch = watchStore.get(i);

        if(watch.getSerialNumber.equals(serialNumber)){
            System.out.print("item found");
            return true;
        }
    } 

    System.out.println("no such record");
    return false;
}

如果需要查找對象,則應添加對象的返回類型。 給出一個清楚地描述您的方法目標的名稱。

public Watch findWatchBySerialNumber (long serialNumber){
    boolean isFound = false;
    Watch watch = null; // declaring variable out of the loop is better. and name of you variable should describe your object, same name is better.
    for(int i = 0; i < watchList.size(); i++){ // your list name should be "watchList".
        watch = watchList.get(i);

        if(fd.getSerialNumber.equals(serialNumber)){
            System.out.print("item found");
            return watch;
        }    
    } 

    System.out.print("item not found");
    return null; // watch is not found.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM