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

I have a generic arraylist object here. I want to delete certain items. The problem is that when I repeat the list with a for loop, I cannot execute a simple remove() sequence, because the elements are moved after each removal.

thanks

+4
source share
6 answers

Use Iterator to remove an item.

how

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

You can iterate over the list this way ...

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

Which is equivalent ...

 public void clean1(List<Kopek> kopeks) { Iterator<Kopek> kopekIter = kopeks.iterator(); while (kopekIter.hasNext()) { Kopek kopek = kopekIter.next(); if (kopek.isDirty()) kopeks.remove(kopek); } } 

Do not do this ... (due to what 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 that deleting by index rather than by object is more efficient. Removing an object is inefficient because the list is in most cases not hashed.

kopeks.remove (penny);

vs

kopeks.remove (i);

To achieve positional removal by treating a moving target accordingly ...

 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++; } } 
+2
source

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

 mArrayList.remove(object); 

or you can use Iterator to delete your objects:

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

You can scroll back and delete when you go through an ArrayList. This has the advantage that subsequent elements do not need to be switched and are easier to program than moving forward.

  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(); } } 
+1
source

Create a separate data index ArrayList to remove from the original ArrayList, then remove these elements by going through it with a for loop.

 ArrayList<Myobj> arr = new ArrayList<Myobj>(); for (Myobj o : arr){ arr.remove(arr.indexOf(o)); } 
0
source

Without using iterators, it also solves the problem. All I wanted to do was get the index that needs to be removed, and sort it in descending order, and 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); 
0
source

All Articles