DEV Community

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

Andrew (he/him) on October 10, 2018

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

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.