Java,如何删除ArrayList中的Integer项 [英] Java, how to remove an Integer item in an ArrayList
本文介绍了Java,如何删除ArrayList中的Integer项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有这样一个ArrayList:
Suppose I have such an ArrayList:
ArrayList<Integer> list = new ArrayList<Integer>();
添加操作之后:
list.add(2);
list.add(3);
list.add(5);
list.add(7);
如果要删除 2号
,我想删除
list.remove(2);
然后将删除 5号
,如何删除 2号
?假设我不知道数字2
的索引.
then number 5
will be deleted, how could I delete number 2
? And suppose I don't know the index of number 2
.
推荐答案
尝试一下
list.removeAll(Arrays.asList(2));
它将删除所有值= 2的元素
it will remove all elements with value = 2
您也可以使用此
list.remove(Integer.valueOf(2));
但它只会删除第一次出现的2
but it will remove only first occurence of 2
list.remove(2)
不起作用,因为它与 List.remove(int i)
匹配,后者会删除具有指定索引的元素
list.remove(2)
does not work because it matches List.remove(int i)
which removes element with the specified index
这篇关于Java,如何删除ArrayList中的Integer项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文