DEV Community

Andrew (he/him)
Andrew (he/him)

Posted on

The int vs. Integer pitfall of Java's ArrayList.remove()

Be careful when you try to use ArrayList.remove() to delete specific elements using a boxed Integer rather than a primitive int -- you probably won't get the result you expect:

jshell> ArrayList alist = new ArrayList(Arrays.asList('a', 'b', 'c', 'd', 'e'))
alist ==> [a, b, c, d, e]

jshell> alist.remove((Integer)3)
$2 ==> false

jshell> alist
alist ==> [a, b, c, d, e]
Enter fullscreen mode Exit fullscreen mode

...this is because ArrayList has two remove() methods. One which takes a java.lang.Object, and one which takes a primitive int.

jshell> alist.remove(3)
$4 ==> 'd'

jshell> alist
alist ==> [a, b, c, e]
Enter fullscreen mode Exit fullscreen mode

When you use the former one, Java tries to match the Integer object in your list, rather than removing the object at the specified index. In most cases, this will not be what you want. Be especially careful when doing things like:

jshell> ArrayList toDelete = new ArrayList(Arrays.asList(0, 2))
toDelete ==> [0, 2]

jshell> for (Integer ii : toDelete) alist.remove(ii)

jshell> alist
alist ==> [a, b, c, e]
Enter fullscreen mode Exit fullscreen mode

Here, we get no hint within the jshell that something went wrong (like we did above when remove() returned false). Make sure to always loop over primitive ints:

jshell> for (int ii : toDelete) alist.remove(ii)

jshell> alist
alist ==> [b, c]
Enter fullscreen mode Exit fullscreen mode

This post originally appeared in a slightly different form on my (now defunct) Wordpress blog.

Discussion (3)

Collapse
djtai profile image
David Taitingfong

Ahh, that is interesting. So...if it's not an int index then it's looking for a specific Object? This seems like a good Java interview question - pretty tricky IMO.

Collapse
awwsmm profile image
Andrew (he/him) Author

Yep! Integer is a subclass of Object, so Java assumes you're looking for a specific Integer object in your ArrayList, even if the actual subclass is something totally different, like Character.

Collapse
aminejava profile image
BOUFATAH Amine • Edited on

Nice article! I've written an article about references object pitfall here: dev.to/aminejava/wrapper-classes-p... check it out ;)