Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...