English 中文(简体)
造成这一例外的情况如何?
原标题:What could be causing this exception?

我有时会遇到非常奇怪的例外:

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.

感谢雷库德为点人!

最佳回答

as of 1.5 the method addAll in arraylist was buggy, it was supposed to get fixed in a future realease, however I m not sure in wich one. To work around this you could create an Iterator for your collection and adding the elements one by one. Here is a little code to help:

Iterator<MyTypeofObjectToAdd> e = myArrayListToCopyFrom.iterator();
    while (e.hasNext()) {
      arrayListToAdd(e.next());
    }

and the IndexOutOfBounds exception is caused because you are trying to retrieve an item from a position where there is nothing.

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签