Logo

dev-resources.site

for different kinds of informations.

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

Published at
10/10/2018
Categories
java
beginners
debugging
pitfalls
Author
awwsmm
Categories
4 categories in total
java
open
beginners
open
debugging
open
pitfalls
open
Author
6 person written this
awwsmm
open
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.

Featured ones: