Reputation: 913
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);
}
}
Upvotes: 22
Views: 53300
Reputation: 5513
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)
Upvotes: 0
Reputation: 654
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);
Upvotes: 0
Reputation: 356
You can call this instead of creating an int
someArrayList.remove(integerKeyLocation.intValue());
Upvotes: 0
Reputation:
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))
Upvotes: 14
Reputation: 8087
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.
Upvotes: 3
Reputation: 13789
int keyLocation = reducedFD.indexOf(KeyPlus.get(KEYindex)); //Use a primitive int
someArrayList.remove(keyLocation);
Upvotes: 1
Reputation: 28568
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);
Upvotes: 93