簡體   English   中英

比較ArrayList中Object的long

[英]Compare long of Object in ArrayList

我正在嘗試使用以下代碼檢查我的 arraylist 是否具有長名稱的對象:

public static boolean containsName(Collection<MyObject> c, long name) {
    for(MyObject o : c) {
        if(o != null && o.getName() == name) { //name is a long
            return true;
        }
    }
    return false;
}

我的對象:

public class MyObject extends SugarRecord {
Long fk_id_specs;
String url;
String timestamp;
int status;
int time;

public MyObject(Long fk_id_specs, String url, String timestamp,int status,int time) {
    this.fk_id_specs = fk_id_specs;
    this.url = url;
    this.timestamp = timestamp;
    this.status = status;
    this.time = time;
}

我正在通過庫從數據庫中獲取集合:

List<MyObject> sp = MyObject.listAll(MyObject.class);

該列表已正確填寫,已經檢查過。

問題是:它總是返回 false,有什么建議嗎?

假設 o.getName() 是一個字符串。 您必須像這樣比較字符串:

String o_name = o.getName();
String str_name = String.valueOf(name); // Convert the long to String
boolean is_equal = o_name.equals(str_name);

暫無
暫無

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

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