第二次例外滚动arraylist

时间:2017-06-14 10:03:06

标签: android for-loop arraylist indexoutofboundsexception

当我尝试删除arrayList上的元素时遇到问题。我需要在其他数组上放置一些元素,然后从原始数组中删除这些元素。我把代码放在下面:

private void setOrderAnswers(int position) {
    for (int i = 0; i < 4; i++) {
        listAnswersAux.add(listAnswers.get((position * 4) + i));
    }

    for (int i = 0; i < 4; i++) {
        listAnswers.remove((position * 4) + i);
    }
}

我遇到的错误是IndexOutOfBounds异常:

Caused by: java.lang.IndexOutOfBoundsException: Index: 10, Size: 10

当我尝试删除此索引时,我不知道为什么会出现此异常,但是当我转到索引来复制元素时,没有问题。我的意思是,索引是正常的,因为我可以在删除之前看到该元素。

我总是在Index值= 10时有例外,我可以在转到index = 10(position = 2)之前完成所有这些操作。

任何人都可以帮助我吗?非常感谢!

06-14 09:58:58.455 31337-31337/com.prodintec.am_motion E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.prodintec.am_motion, PID: 31337
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.prodintec.am_motion/com.prodintec.am_motion.QuizActivity}: java.lang.IndexOutOfBoundsException: Index: 10, Size: 10
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                         at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:154)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                      Caused by: java.lang.IndexOutOfBoundsException: Index: 10, Size: 10
                                                                         at java.util.ArrayList.get(ArrayList.java:411)
                                                                         at com.prodintec.am_motion.QuizActivity.setOrderAnswers(QuizActivity.java:201)
                                                                         at com.prodintec.am_motion.QuizActivity.randomQuestions(QuizActivity.java:168)
                                                                         at com.prodintec.am_motion.QuizActivity.onCreate(QuizActivity.java:48)
                                                                         at android.app.Activity.performCreate(Activity.java:6679)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                         at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:154) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

1 个答案:

答案 0 :(得分:0)

试试这个

private void setOrderAnswers(int position) {
for (int i = 0; i < 4; i++) {
    listAnswersAux.add(listAnswers.get((position * 4) + i));

    listAnswers.remove((position * 4) + i);
}   

}