我有时会遇到非常奇怪的例外:
04-18 18:08:08.121: E/AndroidRuntime(3031): Uncaught handler: thread main exiting due to uncaught exception
04-18 18:08:08.141: E/AndroidRuntime(3031): java.lang.ArrayIndexOutOfBoundsException
04-18 18:08:08.141: E/AndroidRuntime(3031): at java.util.ArrayList.addAll(ArrayList.java:257)
04-18 18:08:08.141: E/AndroidRuntime(3031): at com.zigzagworld.text.formatter.FormatManager.formatIfNeeded(FormatManager.java:185)
etc.
这在一名1.6名支持者身上发生。 我的代码“addAll
的行文是:
mTasksAbove.addAll(schedule.above);
我所能想象的唯一东西是同时进入<代码>。 任务Above。 然而,我非常谨慎地确保所有出入都在同一目标上同步进行。 我想到的只有<代码>附件.above在执行<代码>add
http://www.ohchr.org。
根据Rykud的回答,我做了些微的挖掘,发现了确切的 b。 在安的1.6种来源中,<代码>addAll方法指一种内部方法,即:growAtEnd
。 来源本身:
private void growAtEnd(int required) {
int size = size();
if (firstIndex >= required - (array.length - lastIndex)) {
int newLast = lastIndex - firstIndex;
if (size > 0) {
System.arraycopy(array, firstIndex, array, 0, size);
int start = newLast < firstIndex ? firstIndex : newLast;
Arrays.fill(array, start, array.length, null);
}
firstIndex = 0;
lastIndex = newLast;
} else {
int increment = size / 2;
if (required > increment) {
increment = required;
}
if (increment < 12) {
increment = 12;
}
E[] newArray = newElementArray(size + increment);
if (size > 0) {
System.arraycopy(array, firstIndex, newArray, 0, size);
/********************************************************
* BUG! The next 2 lines of code should be outside the if *
********************************************************/
firstIndex = 0;
lastIndex = size;
}
array = newArray;
}
}
The bug rears its ugly head when the list had been previously populated and then emptied, leaving size == 0
and firstIndex == lastIndex > 0
and you try to add a collection large enough that it doesn t fit between lastIndex
and the current array size.
The bug is gone as of 2.2, when the addAll
method was rewritten and growAtEnd
disappeared from the class.
感谢雷库德为点人!