ArrayList的<整数GT;用get / remove方法整数、方法、LT、ArrayList

2023-09-07 12:50:47 作者:屏息

当我在Java中使用ArrayList中,也有一些事情,我不明白。这是我的初始code:

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

这code是很暧昧。为了澄清什么,我想这样做在code,我指定类型是这样的:

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

如果我不交流,了解哪些方法调用的唯一办法就是把鼠标指针的方法来查看: java.util.ArrayList.remove布尔(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

推荐答案

有关本州。 列表#删除(指数) 返回的对象从列表中删除。 名单#删除(对象) 返回一个布尔值。

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)));