java ArrayList <Integer> 与 get/remove 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15117132/
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
ArrayList <Integer> with the get/remove method
提问by julien dumortier
when I use ArrayList in Java, there are some things that I do not understand. Here is my initialization code:
当我在 Java 中使用 ArrayList 时,有一些我不明白的事情。这是我的初始化代码:
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
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)
如果我没有AC,知道调用哪些方法的唯一方法是将鼠标指针放在方法上查看:java.util.ArrayList.remove boolean (Object object)
Java But how does it make difference? is there a method pointer? Is there a less ambiguous way to do this?
Java 但是它有什么不同呢?有方法指针吗?有没有一种不那么模棱两可的方法来做到这一点?
thank you very much, sorry for my English.
非常感谢,对不起我的英语。
PS: I should say that I finally used SparseIntArray but I am curiously
PS:我应该说我终于使用了 SparseIntArray 但我很好奇
回答by PermGenError
For staters. List#remove(index)returns the Object removed from the list. List#remove(Object)returns a boolean.
对于州人。List#remove(index)返回从列表中删除的对象。List#remove(Object)返回一个布尔值。
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)));
回答by Sebastian
Whenever there is an ambiguity in Java (when multiple method signatures could match the types of the parameters at compile time), using casts to choose the right method is your only choice. Of course you could cast the parameters to local variables before the actual invocation, but this doesn't make it clearer for the reader - casting the parameter directly where it is used is the best option, in my opinion, to make it clear which method is called.
每当 Java 中存在歧义时(当多个方法签名可以在编译时匹配参数的类型时),使用强制转换来选择正确的方法是您唯一的选择。当然,您可以在实际调用之前将参数转换为局部变量,但这并没有让读者更清楚 - 在我看来,直接将参数转换到使用它的地方是最好的选择,以明确哪种方法叫做。
By the way the reason the API is so ambigious in this case is that at the time the API was made, there was no Auto Boxing so it was not possible to write ambiguous code in the first place. However changing the method to make it unambiguous would have been a breaking change. Since storing Integer in an arraylist isn't that great of an idea anyway most of the time, they decided to let us live with that slight annoyance.
顺便说一下,在这种情况下,API 如此模糊的原因是,在创建 API 时,没有自动装箱,因此首先不可能编写模糊的代码。然而,改变方法以使其明确无误将是一个突破性的变化。由于在大多数情况下将 Integer 存储在数组列表中并不是一个好主意,因此他们决定让我们忍受这种轻微的烦恼。
回答by Ramp
If it is just the ambiguity with the remove() method in Integer ArrayList is bothering you, you can extend the ArrayList to implement your own :
如果只是 Integer ArrayList 中的 remove() 方法的歧义困扰您,您可以扩展 ArrayList 以实现您自己的:
public class MyIntArrayList extends ArrayList<Integer> {
boolean removeByObject(Integer intObj) {
return super.remove(intObj);
}
Integer removeByIndex(int index) {
return super.remove(index);
}
}
回答by Revolutionair
You could use list.remove(new Integer(0))
to surely remove the object
你可以list.remove(new Integer(0))
用来肯定地删除对象