gpt4 book ai didi

Java ArrayList 删除对象 - IndexOutOfBoundsException

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:27:04 25 4
gpt4 key购买 nike

我试图从 ArrayList 中删除一个对象,但我一直收到 IndexOutOfBounds 错误。现在有大量可用的信息可以说明为什么在迭代 ArrayList 同时删除 时会发生这种情况,但我没有这样做。示例:

 ArrayList<Character> a = new ArrayList<Character>();
a.add('A');
a.add('B');
a.add('C');
System.out.println(a);
a.remove('A');
System.out.println(a);

打印 [A, B, C] 然后失败:

java.lang.IndexOutOfBoundsException: Index: 65, Size: 3
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.remove(ArrayList.java:474)
at b.<init>(b.java:23)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at bluej.runtime.ExecServer$3.run(ExecServer.java:746)

为什么会这样?

编辑以澄清此问题不是 this question 的副本:

这里发生的具体问题与迭代时删除元素时的常见问题无关。而是由于重载了ArrayList remove 方法和java 将char 自动类型转换为int 造成的。

其他问题的答案中没有涉及这方面。

最佳答案

有 2 个重载的 remove 方法 -- one that takes an int as an index , 和 one that takes an Object , 删除对象引用本身。

Section 15.12.2 of the JLS涵盖了 Java 如何选择一种方法重载而不是另一种方法。

The phases are:

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

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

  1. The third phase (§15.12.2.4) allows overloading to be combined with variable arity methods, boxing, and unboxing.

(大胆强调我的)

这两种方法都适用,因为char可以提升为int,但也可以装箱为Character,匹配 a 的类型参数。但是 Java 会在任何需要装箱的方法之前单独选择提升,因此 'A' 被提升为 int,因此值为 65。

如果您想通过对象引用删除,您可以将它显式转换为 Character

a.remove((Character) 'A');

关于Java ArrayList 删除对象 - IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34643366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com