ArrayList <整数>使用 get/remove 方法 [英] ArrayList <Integer> with the get/remove method
问题描述
当我在Java中使用ArrayList时,有一些我不明白的地方.这是我的初始化代码:
when I use ArrayList in Java, there are some things that I do not understand. Here is my initialization code:
ArrayList<Integer> list = new ArrayList <Integer> ();
list.add (0);
list.add (1);
有时我需要通过索引删除一个对象:
sometimes I need to delete an object by its index:
list.remove (0) // delete the object in the first box
但有时我想按内容删除一个对象:
but sometimes I want to delete an object by its contents:
list.remove (0) // delete the object HAS Which value of 0
这段代码很模糊.为了澄清我想在代码中做什么,我指定了这样的类型:
this code is very ambiguous. To clarify what I want to do it in code, I specify the type like this:
list.remove ((Object) 0) // delete the object which has a value of 0
如果我没有AC,唯一知道调用了哪些方法的方法是将鼠标指针放在方法上查看:java.util.ArrayList.remove boolean (Object 对象)
If I do not AC, the only way to know which methods are called is to put the mouse pointer on the method to see: java.util.ArrayList.remove boolean (Object object)
Java 但是它有什么不同呢?有方法指针吗?有没有更明确的方法来做到这一点?
Java But how does it make difference? is there a method pointer? Is there a less ambiguous way to do this?
非常感谢,对不起我的英语.
thank you very much, sorry for my English.
PS:我应该说我终于使用了 SparseIntArray 但我很好奇
PS: I should say that I finally used SparseIntArray but I am curiously
推荐答案
对于状态者.List#remove(index) 返回从列表中删除的对象.List#remove(Object) 返回一个布尔值.
For staters. List#remove(index) returns the Object removed from the list. List#remove(Object) returns a boolean.
但是在这种特殊情况下.你可以.
In this special case however. you could do .
ArrayList<Integer> list = new ArrayList <Integer> ();
list.add (0);
list.add (1);
System.out.println(list.remove(new Integer(0)));
这篇关于ArrayList <整数>使用 get/remove 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!