22

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

}
}
4
  • 2
    what do you mean "it will not work"? Computer reboots, I assume? Commented Dec 24, 2010 at 5:15
  • 1
    By "it will not work", I mean it does not remove it at all. It ignores it. Commented Dec 24, 2010 at 5:17
  • Can you put your code a bit more detailed? A little bit more code.. Commented Dec 24, 2010 at 5:20
  • so how do you know it doesn't remove it? Commented Dec 24, 2010 at 5:25

8 Answers 8

93

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);
Sign up to request clarification or add additional context in comments.

4 Comments

I was stuck on this for more than an hour.. Thanks dude... :)
+1, for someone new to Java, the idea that int and Integer are different is kind of confusing. Thanks.
this was eaten my 2 hours.thanx
Oh boy, I spent 2 hours figuring out why my object doesn't get removed.....thanks
14

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

Comments

3

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.

Comments

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

Comments

0

Or you could just do:

 someArrayList.remove(keyLocation + 0);

Comments

0

You can call this instead of creating an int

someArrayList.remove(integerKeyLocation.intValue());

Comments

0

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

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.