[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
That's a pleasureAleicia Forsberg wrote:Thank you for the warm welcome . . .
The problem is that you have an overloaded method, and you need to know which version of it is called in which circumstances. You can also try a cast:- numbers.remove((Object)1); See if you can't work out what that doesI see where the misunderstanding might be. . . .
Campbell Ritchie wrote:The problem is that you have an overloaded method, and you need to know which version of it is called in which circumstances. You can also try a cast:- numbers.remove((Object)1); See if you can't work out what that does
What do you think this code outputs?
It actually outputs 1. After adding the two values, the List contains [1, 2]. We then request
the element with index 1 be removed. That’s right: index 1. Because there’s already a remove()
method that takes an int parameter, Java calls that method rather than autoboxing. If you
want to remove the 2, you can write numbers.remove(new Integer(2)) to force wrapper
class use.
Jacob M. Van Laan, Ph.D.
Marquis Software, Inc.