java – Remove from an Integer List

Question:

I have an ImageButton the imgB1 :

When I click on imgB1 , I add to a list of integers, 1. If I click on imagB1 , it removes the 1 and puts the 6.

List<Integer> list = new ArrayList<>();

When I click:

if (ContemLista(v) == false)
{
   list.add(1);
}
else
{
   list.remove(1);
   list.add(6);
}

However, number 1 remains on the list . What am I doing wrong?

Answer:

The remove method of this structure ( List<Integer> list = new ArrayList<>(); ) has two parameters for removal, one is of type Object and the other is the index of the position , in your case it looks like it was removing a position that was not of value 1 but of index 1.

To remove it could be in two ways:

Removing by value:

lista.remove((Object)1);

Removing by the index of the position if you find:

int ret = lista.indexOf(1);
if (ret > -1) lista.remove(ret);

Java and its crazy programming.

References:

Scroll to Top