我试图通过以下代码检查我的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;
}
为MyObject:
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);
列表已正确填写,已经检查过。
问题是:它总是返回错误,有任何建议吗?
答案 0 :(得分:-2)
假设o.getName()是一个String。你必须比较这样的字符串:
String o_name = o.getName();
String str_name = String.valueOf(name); // Convert the long to String
boolean is_equal = o_name.equals(str_name);