Java,如何删除 ArrayList 中的整数项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21795376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:40:58  来源:igfitidea点击:

Java, how to remove an Integer item in an ArrayList

java

提问by Zhenxiao Hao

Suppose I have such an ArrayList:

假设我有这样一个 ArrayList:

ArrayList<Integer> list = new ArrayList<Integer>();

After the adding operation:

添加操作后:

list.add(2);
list.add(3);
list.add(5);
list.add(7);

I want to remove number 2, if I do

我想删除number 2,如果我这样做

list.remove(2);

then number 5will be deleted, how could I delete number 2? And suppose I don't know the index of number 2.

然后number 5会被删除,我怎么能删除number 2?假设我不知道number 2.

采纳答案by Evgeniy Dorofeev

try this

尝试这个

list.removeAll(Arrays.asList(2));

it will remove all elements with value = 2

它将删除所有 value = 2 的元素

you can also use this

你也可以用这个

list.remove(Integer.valueOf(2));

but it will remove only first occurence of 2

但它只会删除第一次出现的 2

list.remove(2)does not work because it matches List.remove(int i)which removes element with the specified index

list.remove(2)不起作用,因为它匹配List.remove(int i)删除具有指定索引的元素

回答by Ashish

list.remove(0);

0 represent element at index 0

0 表示索引 0 处的元素

and you have written list.remove(2);which means remove element at index 2 (i.e element at third place i.e 5 since ArrayListStart with index 0,1,2....)

你写的list.remove(2);意思是删除索引 2 处的元素(即第三个元素,即 5,因为ArrayList从索引 0,1,2.... 开始)

回答by chm

There's no explicit method for finding a particular list element and then removing it. You have to first find it with using the indexOfmethod:

没有明确的方法来查找特定的列表元素然后将其删除。您必须首先使用以下indexOf方法找到它:

int index = list.indexOf(element); // for your example element would be 2
list.remove(index);

Be aware that indexOfreturns the index of the first occurrenceof the object you give it, so you'll have to adjust accordingly for cases where you want to delete an item that is in the list multiple times.

请注意,indexOf返回您给它的对象第一次出现的索引,因此您必须针对要多次删除列表中的项目的情况进行相应调整。

回答by Rohit Jain

There are two versions of remove()method:

方法有两个版本remove()

With an ArrayList<Integer>, removing an integer value like 2, is taken as index, as remove(int)is an exact match for this. It won't box 2to Integer, and widen it.

使用ArrayList<Integer>,删除像2,这样的整数值被视为索引,因为remove(int)它是完全匹配的。它不会框2Integer,并加宽它。

A workaround is to get an Integerobject explicitly, in which case widening would be prefered over unboxing:

一种解决方法是Integer显式获取对象,在这种情况下,扩大比拆箱更受欢迎:

list.remove(Integer.valueOf(2));

回答by badoualy

I think this is what you want : ArrayList <Integer> with the get/remove method

我认为这就是你想要的:ArrayList <Integer> 与 get/remove 方法

list.remove(new Integer(2));

回答by badoualy

try this:

尝试这个:

list.remove(list.indexOf(2));

回答by Rakesh KR

Try,

尝试,

list.remove(0);
  1. remove(int index)

    Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.

  2. remove(Object o)

    Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

  1. 删除(整数索引)

    移除此列表中指定位置的元素(可选操作)。将任何后续元素向左移动(从它们的索引中减去一个)。返回从列表中删除的元素。

  2. 删除(对象 o)

    从此列表中删除第一次出现的指定元素(如果存在)。如果列表不包含该元素,则它保持不变。更正式地,删除具有最低索引 i 的元素,使得 ( o==null ? get(i)==null : o.equals(get(i))) (如果存在这样的元素)。如果此列表包含指定的元素(或等效地,如果此列表因调用而更改),则返回 true。

回答by Abdalrhman Alkhulaqi

Simply if you use method like this it will remove element at index 2

简单地,如果您使用这样的方法,它将删除索引 2 处的元素

YOUR ARRAYLIST: 2,3,5,7

你的数组列表:2,3,5,7

list.remove(2);

list.remove(2);

OUTPUT: 2,5,7

输出:2,5,7

And if you use method like this it will remove element with value 2

如果您使用这样的方法,它将删除值为 2 的元素

YOUR ARRAYLIST: 2,3,5,7

你的数组列表:2,3,5,7

list.remove(Integer.valueOf(2));

list.remove(Integer.valueOf(2));

OUTPUT: 3,5,7

输出:3,5,7

Hope it help...

希望它有帮助...

回答by wired00

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

这将转换为 Integer 对象而不是原始对象,然后转换remove()为 Object 而不是 Arraylist Index