简体   繁体   中英

ArrayList.remove not working with Integer, works with constant

Alright, I'm new to Java, I'm just working through a class, and I've hit a bit of a snag on a program for class. I've managed to work my way through every bit of my final program, except for this last thing.

public static void remove(String studentID)
{
Integer foundLocation = 0;

for (int i = 0; i < studentList.size(); i++)        
    {
        if (studentList.get(i).getStudentID().compareTo(studentID) == 0)
            {
                //This means we have found our entry and can delete it
                foundLocation = i;

            }
    }
System.out.println(foundLocation);
if (foundLocation != 0)
    {
        System.out.println(studentList);

        studentList.remove(foundLocation);
        System.out.println(foundLocation.getClass().getName());


        System.out.println("Student ID removed: " + studentID);
        System.out.println(studentList);
    }
else
    {
        System.out.println("Sorry, " + studentID + " not found.");
    }

The code seems like it should work. But, what I get is that the remove doesn't actually do anything. My extra prints are there to verify. The ArrayList just plain doesn't change.

However, if I just replace:

studentList.remove(foundLocation);

with something like:

studentList.remove(3);

It just removes perfectly.

foundLocation is an Integer .

Can someone explain to me what I've got going on here?

I expect it's blindingly obvious to someone familiar with Java, but I'm missing it.

This is a bit of a nasty overload that snuck into the Collections API design.

There are two remove methods, one that you call with an int , and one that you call with an Object , and they do very different things.

Unfortunately for you Integer is also an Object , even though you want to use it as an int (and do that in a couple of other places, thanks to the magic of autoboxing that unfortunately does not work for remove ).

remove(1) will remove by index (the 2nd element).

remove(Integer.valueOf(1)) will remove the object by its value (the first "1" found in the list).

It would have probably been wiser to give these two methods two different names.

In your case, change foundPosition to be an int .

ArrayList有两个remove方法,一个是remove(int index) ,另一个是remove(Object object) ,您的foundLocation类型是Integer ,当使用它时将是一个引用,因此当您调用remove(foundLocation)时它将调用remove(Object) ,尝试找到一个元素== foundLocation,找不到它,因此什么也不要删除,一旦将类型更改为int,它将删除索引foundLocation处的元素,请参考doc方法。

There are two 'remove' methods in the ArrayList class. One accepts an Object type, the other accepts a int type. By using the Integer object, you are finding an element in the list that is equals to the Integer object. However when you remove by an int type, you are moving by the position of the element in the list.

studentList.remove(foundLocation) will result in the ArrayList checking for a Integer object that is equal to the one that is referenced by foundLocation. This is an object equality check. Two different Integer objects with the same value is deemed as being different even though they have the same numeric value.

studentList.remove(3) will result in the ArrayList to remove the fourth element in the list.

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