Java, how to delete integer items in ArrayList
•
Java
Suppose I have such an ArrayList:
ArrayList<Integer> list = new ArrayList<Integer>();
After adding operations:
list.add(2); list.add(3); list.add(5); list.add(7);
I want to delete No. 2 if I do
list.remove(2);
Then No. 5 will be deleted. How can I delete No. 2? Suppose I don't know the index of No. 2
Solution
Try this
list.removeAll(Arrays.asList(2));
It will delete all elements with value = 2
You can also use this
list.remove(Integer.valueOf(2));
But it will only delete the first occurrence 2
list. Remove (2) does not work because it matches list Remove (int i), which deletes the element of the specified index
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码