How to remove consecutive elements from Java ArrayList?

I am a relatively new Java programmer, and I find it difficult to remove more than one element from an ArrayList. Ideally, I would like to do something like this:

ArrayList ar1 = new ArrayList(); ar1.add(...) ar1.add(...) ar1.add(...) ar1.add(...) for (int i = 0; i < 2; i++){ ar1.remove(i); } 

I think an iterator might help, but I can't find an example that is close to what I'm trying to do. Any help would be greatly appreciated. Thanks.

+6
java arraylist
source share
6 answers

Here you want:

 ar1.subList(0, 2).clear(); 

This creates a sublist view of the first two elements of the list, and then clears this sublist, removing them from the original list. The subList method exists mainly for this kind of thing ... performing operations on a specific range of the list.

+20
source share

Can you do this

  ArrayList ar1 = new ArrayList(); ar1.add("a"); ar1.add("b"); ar1.add("c"); ar1.add("d"); for (int i = 0; i < 2; i++) { ar1.remove(i); } System.out.println(ar1); 

Note that after deleting the first element, other elements change. Thus causing

 ar1.remove(0); ar1.remove(1); 

will effectively remove the first and third items from the list. This will remove the first two elements:

 ar1.remove(0); ar1.remove(0); 
+5
source share

For indexed abstractions from a list, you need to consider the opposite:

  for (int i = 1; i >= 0; i--) 

otherwise, your first deletion shifts the items "higher" in the collection, and you do not complete the deletion of elements that you think are being deleted.

+3
source share

You can use Collection.removeAll (toRemove) if you have a separate list of objects to delete.

http://download.oracle.com/javase/6/docs/api/java/util/Collection.html

If your collection is indexed based on, for example, an ArrayList, you can call

 remove(index) 

to remove an item in the index. You can do this in a loop, but be careful that deleting shifts all indexes, as another answer indicates.

If all you want to do is remove the first two items from the list, then

  list.remove(0); list.remove(0); 

must do it.

+2
source share

If you know the indices of the elements that you want to delete, you can delete them in the reverse order without worrying about offset indexes:

  ArrayList ar1 = new ArrayList(); ar1.add("a"); ar1.add("b"); ar1.add("c"); ar1.add("d"); int[] indexesToRemove = {0,2,3}; Arrays.sort(indexesToRemove); for (int i=indexesToRemove.length-1; i>=0; i--) { ar1.remove(indexesToRemove[i]); } 
+2
source share

You can try the following:

 List<Whatever> l = new ArrayList<Whatever>(); l.add(someStuff); Iterator<Whatever> it = l.iterator(); int i = 0; while (i < 2 && it.hasNext()) { it.next(); it.remove(); i++; } 

Or more generally:

 List<Whatever> l = new ArrayList<Whatever>(); l.add(someStuff); Iterator<Whatever> it = l.iterator(); while (it.hasNext()) { Whatever next = it.next(); if (shouldRemove(next)) { it.remove(); } } 

EDIT: I think it depends on whether you are trying to delete specific indexes or specific objects. It also depends on how much logic you need to decide if something needs to be removed. If you know the indexes, then delete them in reverse order. If you have a set of objects that you want to remove, use removeAll. If you want to iterate over the list and remove objects matching the predicate, use the code above.

+1
source share

All Articles