Remove from Integer List

Asked

Viewed 196 times

3

I have a ImageButton the imgB1:

When I click on imgB1, add to a list integer, 1. If you click again imagB1, he removes 1 and puts 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 continues on list. What I’m doing wrong?

3 answers

4

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

To remove could then be in two ways:

Removing for the value:

lista.remove((Object)1);

Removing by the position index is found:

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

Java and its crazy programming.

References:

  • I do not think overload is something crazy, quite the contrary, it is usually very useful in some situations. A detail, has a "1" left in lista.remove((Object)1);

  • @diegofm I respect your opinion, did not understand the part of the remaining?

  • My mistake, I’m sorry, it’s not enough.

  • tranquil @diegofm

4

The interface List<Integer> Java has 2 methods remove:

Integer java.util.List.remove(int index): removes the element from the list in the index (index) specified. If it exists, the value no index is returned, otherwise returns null;

boolean java.util.List.remove(Object o): removes the specified object from the list. If it exists, it returns true, otherwise, returns false.

The evoked method will always be the one that best matches the parameter passed. In your case, you are passing a int, thus the method evoked is the remove(int index).

For you to evoke the method remove(Object o), you should specifically pass a Integer thus lista.remove(Integer.valueOf(1)).

Sample code:

public class RemoveFromListInteger {

    public static void main(String[] args) {
        List<Integer> lista = new ArrayList<>();
        lista.add(1);
        lista.add(2);
        lista.add(3);
        lista.add(4);

        lista.remove(1);                    //Remove na posição 1, ou seja, o valor 2
        System.out.println("Depois de remover na posição 1");
        lista.forEach(System.out::println);

        lista.remove(Integer.valueOf(1));   //Remove o valor 1
        System.out.println("\n\nDepois de remover o valor 1");
        lista.forEach(System.out::println);
    }
}

0

When you use the remove of the interface List you pass an index to remove as parameter or value. In your case it may be that it has more than a value 1 in the list and if yes it is removing the first occurrence or else it is always removing index 1 depends on which method you called, itere your list and do the following:

for (Integer num : list) {
            if (num == 1) {
                list.remove(Integer.valueOf(1));
            }
        }
    }

It’s just one of the options.

  • 1

    Douglas, it doesn’t work: list.remove(1) would simply remove the second position from the list, beware of ConcurrentModificationException when removing from a list you are iterating on as well.

  • Fact @Anthonyaccioly I will fix.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.