I am trying to remove an object from an ArrayList, but I keep getting an IndexOutOfBounds Error. Now there is plenty information available why this happens when iter
Should pass index to the remove method of Arraylist. trying replacing this line a.remove('A'); with
a.remove(0);
There are 2 overloaded remove
methods -- one that takes an int as an index, and one that takes an Object, to remove the object reference itself.
Section 15.12.2 of the JLS covers how Java chooses one method overload over another.
The phases are:
- The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.
This guarantees that any calls that were valid in the Java programming language before Java SE 5.0 are not considered ambiguous as the result of the introduction of variable arity methods, implicit boxing and/or unboxing. However, the declaration of a variable arity method (§8.4.1) can change the method chosen for a given method method invocation expression, because a variable arity method is treated as a fixed arity method in the first phase. For example, declaring m(Object...) in a class which already declares m(Object) causes m(Object) to no longer be chosen for some invocation expressions (such as m(null)), as m(Object[]) is more specific.
- The second phase (§15.12.2.3) performs overload resolution while allowing boxing and unboxing, but still precludes the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the third phase.
This ensures that a method is never chosen through variable arity method invocation if it is applicable through fixed arity method invocation.
- The third phase (§15.12.2.4) allows overloading to be combined with variable arity methods, boxing, and unboxing.
(bold emphasis mine)
Both methods are applicable here, because a char
can be promoted to an int
, but it can also be boxed to a Character
, matching a
's type parameter. But Java will choose promotion alone before any method that requires boxing, so 'A'
is promoted to int
, hence the value 65.
You can cast it explicitly to Character
if you want to remove by object reference.
a.remove((Character) 'A');
The remove
method expects an index and it removes an item at the specified index. You are passing a char, thus it is converted to an integer 65.
a.remove()
expects to get an integer for the index of the item to be removed. 'A' returns the ASCII value of A which is 64,thus out of the bounds of the array
The ArrayList
class has two overloads of the method remove
. One with an integer parameter, that removes the item at that index, and one with an Object
parameter.
You are passing a char
. This is neither an int
not an Object
. So the compiler has to decide which remove
it uses: Does it promote the char
to an int
, or does it box it into a Character
which will then be promoted to Object
.
Overload resolution in Java always starts without consideration of boxing and unboxing. Therefore it gives priority to the remove(int)
overload. So it takes the value of the character A
, which is 65, and promotes it to int, which means it will try to remove item #65, when the list only has three items.
To solve this, you need to explicitly tell it to use a Character
object, as in: remove(Character.valueOf('A'))
.