我有两个自创建类的objects.After比较数组列表,我正在从这两个类中删除公共对象,并准备了获取公共elements.After的方法--查找公共元素--我通过调用removeAll()方法删除公共元素,但它不是删除该元素。我以前使用过这个方法,但从未得到过htis类型的error.Here是我的代码:
public void SynchphoneBookwithDB() {
//phone contacts
ArrayList < Contacts > phone_contacts = CommonUtility.getAllContacts(ctx);
Log.e("SynchphoneBookwithDb", "phonebooksize" + phone_contacts.size());
//get data base contacts
ArrayList < Contacts > db_contacts = UserService.getUserServiceInstance(ctx).getNameNumberIdIsmycontactIsBlockedFromContatcsTable();
Log.e("SynchphoneBookwithDb", "DBSIZE" + db_contacts.size());
//get common contacts
ArrayList < Contacts > common_contacts = getCommonContacts(phone_contacts, db_contacts, false);
Log.e("SynchphoneBookwithDb", "common_contacts" + common_contacts.size());
//not operation on common numbers so remove them
phone_contacts.removeAll(common_contacts);
db_contacts.removeAll(common_contacts);
//remained in phone must be added to db
Log.e("SynchphoneBookwithDb", "afetr removing contacts phonebooksize" + phone_contacts.size());
Log.e("SynchphoneBookwithDb", "after removing contacts DBSIZE" + db_contacts.size());
}下面是我获取公共元素的方法:
public ArrayList < Contacts > getCommonContacts(ArrayList < Contacts > referenced, ArrayList < Contacts > other, Boolean insertInDb) {
// Log.d("Inside common contacts","start");
ArrayList < Contacts > commonArrayList = new ArrayList < Contacts > ();
int count_ref = referenced.size();
for (int i = 0; i < count_ref; i++) {
int count_other = other.size();
for (int j = 0; j < count_other; j++) {
if (referenced.get(i).getNumber().equals(other.get(j).getNumber())) {
commonArrayList.add(other.get(j));
if (insertInDb) { //insert from server as it is
other.get(j).setIsmycontact(true);
long k = UserService.getUserServiceInstance(ctx).addInContatcs(other.get(j));
}
}
}
}
return commonArrayList;
}发布于 2013-10-31 09:46:11
你的目标似乎不平等!只是他们的号码场。
重写联系人类的相等方法,并使用它来比较它们。
ArrayList#removeAll(收藏)使用List#remove(对象),它使用相等
更正式地说,删除索引i最低的元素,以便(o==null?get(i)==null :o.equals(get(I)(如果存在这样的元素)。
https://stackoverflow.com/questions/19702997
复制相似问题