是Java的新手,面临arraylist的问题,我不知道我的“ for循环”出了什么问题,无法退出它,并产生错误
ArrayList<String> r2 = new ArrayList<String>();
for(int i=0; i<= idx.length; i++) {
ArrayList<String> r = db.Fetch(idx[i],exta);
if(r.size() != 0) {
for (String s : r) {
r2.add(s);
Log.d("test","ID "+idx[i]+ " :" + s);
}
}
}
当我运行它时,我会在Log.d上获得正确的值,但循环不会退出它
所以帮忙
最佳答案
for(int i=0; i<= idx.length; i++)
ArrayList的索引从
0
到idx.lenght -1
。在idx.lenght
索引上循环将导致ArrayIndexOutBoundExeception
。改变它 for(int i=0; i < idx.length; i++)