How do you remove an int from an ArrayList in Java?
Ngày đăng:
02/12/2021
Trả lời:
0
Lượt xem:
204
Java, how to remove an Integer item in an ArrayListtry this list.removeAll(Arrays.asList(2));it will remove all elements with value = 2 you can also use this list.remove(Integer.valueOf(2));but it will remove only first occurence of 2 list.remove(2) does not work because it matches List.remove(int i) which removes element with the specified index There are two versions of remove() method:
With an ArrayList A workaround is to get an Integer object explicitly, in which case widening would be prefered over unboxing: instead of: list.remove(Integer.valueOf(2));you can of course just use: list.remove((Integer) 2);This will cast to an Integer object rather than primitive and then remove() by Object instead of Arraylist Index |