如何从 Android (Java) 中的通用数组列表中删除项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11429041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:05:20  来源:igfitidea点击:

How to remove items from generic arraylist in Android (Java)

javaandroid

提问by Rakshi

I have a generic arraylist of an object here I want to remove certain elements, The problem is when I iterate the list with for loop, I can't do a simple sequence of remove()'s because the elements are shifted after each removal.

我在这里有一个对象的通用数组列表,我想删除某些元素,问题是当我用 for 循环迭代列表时,我无法执行简单的remove()'序列,因为每次删除后元素都会移动。

Thanks

谢谢

回答by Samir Mangroliya

Use Iteratorto remove element

使用迭代器删除元素

Like

喜欢

Iterator itr = list.iterator();
String strElement = "";
while (itr.hasNext()) {
    strElement = (String) itr.next();
    if (strElement.equals("2")) {
        itr.remove();
    }
}

回答by Blessed Geek

You can iterate the list this way ...

您可以通过这种方式迭代列表...

public void clean(List<Kopek> kopeks) {
  for(Kopek kopek : kopeks) {
    if (kopek.isDirty())
      kopeks.remove(kopek);
  }
}

Which is equiv to ...

这相当于...

public void clean1(List<Kopek> kopeks) {
  Iterator<Kopek> kopekIter = kopeks.iterator();

  while (kopekIter.hasNext()) {
    Kopek kopek = kopekIter.next();
    if (kopek.isDirty())
      kopeks.remove(kopek);
  }
}

Don't do this ... (due to the reason you have already observed.)

不要这样做......(由于您已经观察到的原因。)

public void clean(List<Kopek> kopeks) {
  for(int i=0; i<kopeks.size(); i++) {
    Kopek kopek = kopeks.get(i);
    if (kopek.isDirty())
      kopeks.remove(i);
  }
}

However, I believe removal by index rather than by object is more efficient. Removal by object is not efficient because the list is in most cases not a hashed list.

但是,我相信按索引而不是按对象删除更有效。按对象删除效率不高,因为列表在大多数情况下不是散列列表。

kopeks.remove(kopek);

kopeks.remove(kopek);

vs

对比

kopeks.remove(i);

kopeks.remove(i);

To achieve positional remove, by treating a moving target appropriately ...

为了实现位置移除,通过适当地处理移动目标......

public void clean(List<Kopek> kopeks) {
  int i=0;
  while(i<kopeks.size()) {
    Kopek kopek = kopeks.get(i);
    if (kopek.isDirty()) // no need to increment.
      kopeks.remove(i);
    else
      i++;
  }
}

回答by amicngh

You could iterate backwardsand remove as you go through the ArrayList. This has the advantage of subsequent elements not needing to shift and is easier to program than moving forwards.

您可以在遍历ArrayList 时向后迭代并删除。这具有后续元素不需要移动的优点,并且比向前移动更容易编程。

   List<String> arr = new ArrayList<String>();
   ListIterator<String> li = arr.listIterator(arr.size());

    // Iterate in reverse.
    while(li.hasPrevious()) {

        String str=li.previous();
        if(str.equals("A"))
        {
            li.remove();
        }
    }

回答by Ovidiu Latcu

If you have the objects that you want to remove from your ArrayList<T>you can use :

如果您有要从中删除的对象,ArrayList<T>可以使用:

mArrayList.remove(object);

or you can use an Iteratorto remove your objects:

或者您可以使用 anIterator删除您的对象:

while(iterator.hasNext()){
    if(iterator.next() == some condition for removal){
        iterator.remove();
    }
}

回答by Kumar Vivek Mitra

Create a separate ArrayList of Index of the data to be removed from the original ArrayList, then remove those elements by looping over it with for loop.

为要从原始 ArrayList 中删除的数据创建一个单独的 ArrayList ,然后通过使用 for 循环遍历它来删除这些元素。

ArrayList<Myobj> arr = new ArrayList<Myobj>();

for (Myobj o : arr){

  arr.remove(arr.indexOf(o));

 }

回答by Rakshi

without using iterators also solves the issue.. All i wanted to do is get the index which are to be deleted and sort it in decending order then remove it from the list. check the code below

不使用迭代器也解决了这个问题..我想做的就是获取要删除的索引并按降序对其进行排序,然后将其从列表中删除。检查下面的代码

Arraylist<obj> addlist = getlist();
List<Integer> indices = new ArrayList<Integer>();
    for(int i=0; i<addlist.size() ;i++){
        if(addlist.get(i).getDelete()){
            indices.add(i);

        }
    }
    Collections.sort(indices, Collections.reverseOrder());
    for (int i : indices)
        addlist.remove(i);