我不知道這是怎麼發生的,也許我錯過了一些簡單的東西。索引超出界限,但不是?
我想如果有這樣的代碼存在,除去第一指數:
ArrayList<String> historyValues = new ArrayList<String>();
ArrayList<String> historyLabels = new ArrayList<String>();
some_big_process_which_populates_array()
if(historyLabels.size() >= 1 && historyValues.size() >= 1){
historyLabels.add("Not enough data yet");
historyValues.add("0.0");
}else{
Log.v("history_values", historyValues.toString());
historyValues.remove(0);
historyLabels.remove(0);
}
和錯誤。空陣列如何通過size()
檢查?
06-18 15:36:53.208 1510815108/com.rainforestautomation.android.energyvue V/history_values﹕ []
06-18 15:36:53.208 15108-15108/com.rainforestautomation.android.energyvue D/AndroidRuntime﹕ Shutting down VM
06-18 15:36:53.209 15108-15108/com.rainforestautomation.android.energyvue E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.rainforestautomation.android.energyvue, PID: 15108
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
編輯:
看來我扭轉我的邏輯。
你正試圖從一個空的列表中刪除的項目。 –
'historyValues.remove(0);'如果你的'ArrayList'爲空則失敗。沒有魔術背後。 *「空數組如何超過size()檢查?」*它不在,它在'else'分支中,這意味着它無法檢查大小。 – Tom