Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my ArrayList.remove(id) call not working?

I have an ArrayList, which includes a number of items I want to remove. I have the ids of the items to remove stored in another list. Figured the following code should work trivially, but for some reason, the remove() calls are returning a false value:

  ArrayList<Integer> toRemove = new ArrayList<Integer>();
  ArrayList<JCheckBox> al = new ArrayList<JCheckBox>();

  /* Code that adds a bunch of items to al, and a few integers to toRemove */

  System.out.println("Size before removing: " + al.size());
  for (int i = toRemove.size() - 1; i >= 0; i--) {
    System.out.println("Removing id: " + toRemove.get(i) + ": ");
    System.out.println(al.get(toRemove.get(i)));
    System.out.println(al.remove(toRemove.get(i)));
  }
  System.out.println("Size after removing: " + al.size());

I'd get it if the get() call also returned a false value, but it does actually return the object in question. What am I missing here?

The output of the code above:

Size before removing: 3
Removing id: 2: 
javax.swing.JCheckBox[...]
false
Size after removing: 3
like image 642
zigdon Avatar asked Aug 26 '10 22:08

zigdon


People also ask

Does ArrayList support remove?

Java List remove() method is used to remove elements from the list. ArrayList is the most widely used implementation of the List interface, so the examples here will use ArrayList remove() methods.

How do you remove numbers from an ArrayList?

There are 3 ways to remove an element from ArrayList as listed which later on will be revealed as follows: Using remove() method by indexes(default) Using remove() method by values. Using remove() method over iterators.

What happens to an ArrayList when you remove?

If you call ArrayList. remove() the element at the given index is removed from the list (and all elements following it move down one index). The object itself still exists, no memory has been freed yet.

How does ArrayList remove method work?

The remove() method is used to remove an element at a specified index from ArrayList. Shifts any subsequent elements to the left (subtracts one from their indices). The index of the element to be removed.


2 Answers

My guess is you are having a problem with the fact that remove() is overloaded with both int and Object, while get() only takes an int. Try remove(toRemove.get(i).intValue()).

remove(Object) from AbstractCollection will search through the list and remove the given object, which won't be there because you are sending it an Integer and the list only has JCheckBoxs. You are trying to call remove(int), but because you are giving it an Integer, the Object overload is called instead. By converting the Integer to an int, you avoid this problem

Also, can you always be sure the Id in toRemove always equals the index? If toRemove is not in greatest to least order, it won't be.

like image 135
ILMTitan Avatar answered Sep 28 '22 06:09

ILMTitan


There are two problems with your code. First, the wrong "toRemove" method is getting invoked. When you call "toRemove.get(i)", the return value is autoboxed into a java.lang.Integer, instead of an int. Therefore, java.util.List#remove(Object) is called instead of java.util.List#remove(int). It's trying to remove an Integer object, and returns false. If you cast the Integer to an int, the desired method will get invoked.

Second problem: each time you remove an element of the list, the indexes of all subsequent elements change, as those elements are "shifted" down. There are several ways to get around this. One is to sort your list of indexes in descending order. Another would be to use a set of indexes, create a new array, and copy into the new array only those elements whose index is not in the set.

like image 27
dhm Avatar answered Sep 28 '22 06:09

dhm