首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >removeAll()不移除对象

removeAll()不移除对象
EN

Stack Overflow用户
提问于 2013-10-31 09:43:30
回答 1查看 1.1K关注 0票数 1

我有两个自创建类的objects.After比较数组列表,我正在从这两个类中删除公共对象,并准备了获取公共elements.After的方法--查找公共元素--我通过调用removeAll()方法删除公共元素,但它不是删除该元素。我以前使用过这个方法,但从未得到过htis类型的error.Here是我的代码:

代码语言:javascript
复制
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());

}

下面是我获取公共元素的方法:

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-31 09:46:11

你的目标似乎不平等!只是他们的号码场。

重写联系人类的相等方法,并使用它来比较它们。

ArrayList#removeAll(收藏)使用List#remove(对象),它使用相等

更正式地说,删除索引i最低的元素,以便(o==null?get(i)==null :o.equals(get(I)(如果存在这样的元素)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19702997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档