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);
}
}
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);
– user28595
@diegofm I respect your opinion, did not understand the part of the remaining?
– novic
My mistake, I’m sorry, it’s not enough.
– user28595
tranquil @diegofm
– novic