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

Categories

0 votes
445 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...