The interface List<Integer>
of 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 in index
is returned, otherwise it returns null
;
boolean java.util.List.remove(Object o)
: Removes the specified object from the list. If it exists, it returns true
, otherwise it returns false
.
The evoked method will always be the one that best matches the passed parameter. In your case, you are passing int
, so the evoked method is remove(int index)
.
For you to evoke the remove(Object o)
method, you should specifically pass Integer
accordingly 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);
}
}