简体   繁体   中英

Java ArrayList.remove() problem

This is part of my code.

Integer keyLocation = reducedFD.indexOf(KeyPlus.get(KEYindex));
someArrayList.remove(keyLocation);

So what I am doing here is I assign keyLocation(the first occurence of a string in the reducedFD arrayList). But when I want to remove from someArrayList the item with that keyLocation, it will not work.

If I input manually:

someArrayList.remove(0); //Let's say 0 is the actual keyLocation

This actually works.

What is weird is THAT THE FOLLOWING CODE ALSO WORKS:

someArrayList.remove(keyLocation + 1);

Any hints?

Here is the main loop:

for (int KEYindex = 0; KEYindex < KeyPlus.size(); KEYindex++){

 Integer keyLocation = reducedFD.indexOf(KeyPlus.get(KEYindex));

if (reducedFD.contains(KeyPlus.get(KEYindex))){

 KeyPlus.add(reducedFD.get(keyLocation+1));
 CheckedAttributesPlus.add(KeyPlus.get(KEYindex));
 reducedFD.remove(keyLocation);

}
}

The problem is you are passing an Integer to the remove method, and not an int. When you pass an Integer, it assumes that the Integer itself is what you are trying to remove, not the value at that index. Compare the methods

remove(Object o)
remove(int i)

so do:

int keyLocation = reducedFD.indexOf(KeyPlus.get(KEYindex));
someArrayList.remove(keyLocation);

Here is short description :

remove(Object o) // remove object
remove(int index) // remove the object in that index

if you write .remove(5) compiler understands it as a primitive type so as a index and remove index(5). If you want to remove object you should write .remove(new Integer(5))

The List interface has two remove() methods, one that receives an Object (and tries to remove this object from the list) and another that receives an int (and tries to remove the object whose index is the given int).

Usually, giving a method an Integer parameter results in auto-unboxing, or automatic transform to a primitive int . In this case, the compiler will not attempt auto-unboxing, because there's a perfectly good method there that receives Object , and Integer instanceof Object ... I'm guessing your list is not List<Integer> , which is why it fails spectacularly.

Adding an int to your Integer forces auto-unboxing, and the addition results in an int - perfect for the other remove() method.

    int keyLocation = reducedFD.indexOf(KeyPlus.get(KEYindex)); //Use a primitive int
    someArrayList.remove(keyLocation);

或者你可以这样做:

 someArrayList.remove(keyLocation + 0);

你可以调用它而不是创建一个int

someArrayList.remove(integerKeyLocation.intValue());

If you look at ArrayList JavaDoc, you'll find this declaration.

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
    public E remove(int index){
    .
    .
    .
}

Integer is different than int . Does the following make sense?

someArrayList.remove((int)keyLocation);

To make it more clear let's understand with an example. Don't read again if you get confused on the first read!!

Think, this is the list ( <object> ) I have:

one
two
four
five
six

int

int x = 3;
remove(x);   //this will remove "five"

Integer

Integer x = 3;
remove(x);   //this will remove nothing

String

String x = "three"
remove(x);   //this will remove nothing (as "three" is not there)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM