本文介绍了比较动态的ArrayList ArrayList中带!并删除,不能在动态数组present元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个的ArrayList contactModels 列表

contactModels 是一个动态的ArrayList,我需要比较两个列表和删除元素列表,这是不以present contactModels (DynamicArrayList)。

The contactModels is a Dynamic Arraylist, I need to compare the two list and remove the elements in list, which are not present in contactModels(DynamicArrayList).

我试过嵌套的循环,这:

I tried nested loops, and this:

  for (int i = 0; i < list.size(); i++)
  {    
    if(!contactModels.get(i).getEmpID().equals(list.get(i).getEmpID()))
       {
        databaseadapter.removeContact(contactModels.get(i));
       }

  }

但我不能达到它。

But I can't achieve it.

推荐答案

你是不是在 contactModels 测试是否一个项目是不是在<$ C $ present C>列表。相反,你在 contactModels 在一个索引present测试是否该项目不具有相同的ID为同一指数在列表中的项目

you are not testing whether an item in contactModels is not present in list. instead you are testing whether the item at an index present in contactModels not has the same id as the item at the same index in list.

如果两个集合相对于该ID的排序这仅适用,如果contactModels至少有尽可能多的条目列表。

this only works if both collections are sorted with respect to the id's and if contactModels has at least as much entries as list.

是适合你的呢?否则,这可能是你的问题。

is that the case for you ? otherwise this might be your problem.

如果您的收藏中的项目有等于散code 正确实现,并且是如果相等的ID是平等的,你可以使用类似这样

if the items in your collections have equals and hashcode correctly implemented and are equal if their id's are equal you could use something like this

for (<TypeOfYourItems> item : list)
  {    
    if(!contactModels.contains(item))
       {
        databaseadapter.removeContact(item);
       }
  }

这篇关于比较动态的ArrayList ArrayList中带!并删除,不能在动态数组present元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 09:38