2017-06-14 86 views
0

我有一個問題,當我嘗試刪除arrayList上的元素。我需要在其他數組上放置一些元素,然後從原始數組中刪除這些元素。我把下面的代碼:異常滾動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 

我不知道爲什麼我有這樣的例外,當我嘗試刪除該索引,但是當我去索引複製元素,沒有問題。我的意思是,索引是確定的,因爲在刪除它之前我可以看到該元素。

而且我總是有索引值= 10的例外,在索引= 10(位置= 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)  
+0

也許是因爲你已經刪除了10元在第一次運行? –

+0

@Imrik你能告訴我你的名單的大小以及你使用的是什麼類型的名單? – Jeeva

+0

@imrik IndexOutOfBoundsException - 拋出以指示某種類型的索引(例如數組,索引或向量)超出範圍。根據Java文檔。 – Jeeva

回答

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); 
} 

} 
+0

我嘗試了這一點,它再次打破同樣的例外。 – Imrik

+0

同樣的例外是因爲你的數組列表大小 –

+0

是的,因爲索引是10,大小是10.但我不知道它爲什麼會正確執行第一行,而第二行沒有...索引是相同的。 – Imrik

相關問題