Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

java - ArrayList <Integer> with the get/remove method

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

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 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: I should say that I finally used SparseIntArray but I am curiously

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...