我刚刚收到了java.lang.IndexOutOfBoundsException:但无法理解它是由什么造成的。基本上,我正在将sqlite表的记录集成到listview中。 这是我的SQL查询,用于从表中获取记录。
//Your array
var array = {"noun":{"syn":["leap","saltation","startle","start","parachuting","jumping","actuation","descent","inborn reflex","increase","innate reflex","instinctive reflex","physiological reaction","propulsion","reflex","transition","unconditioned reflex"]},"verb":{"syn":["leap","bound","spring","startle","start","leap out","jump out","stand out","stick out","rise","climb up","jump off","derail","chute","parachute","jumpstart","jump-start","pass over","skip","skip over","alternate","alter","appear","assail","assault","attack","change","climb","dive","drop","enter","go","leave out","locomote","look","miss","mount","move","neglect","omit","overleap","overlook","participate","plunge","plunk","pretermit","seem","set on","shift","start up","switch","travel","vary","wax"],"rel":["leap out","jump on"]}};
//The list you want to iterate
var words = array.noun.syn;
//Iterating in the array
for (i=0; i<words.length; i++) {
//Words[i] is every value return after leap (including itself)
console.log(words[i]);
}
这是我检索数据的方法: -
public ArrayList<Integer> getRecordsCount(int no) {
ArrayList<Integer> count = new ArrayList<>();
Cursor c = database.rawQuery(
"select count(*) as TotalCount, tDate, " +
"strftime('%d', tDate) as DayPart, " +
"strftime('%m', tDate) as MonthPart, " +
"strftime('%Y', tDate) as YearPart " +
"from library " +
"where type = '" + no + "' " +
"group by MonthPart, YearPart", null);
while (c.moveToNext()) {
count.add(c.getInt(0));
}
return count;
}
但是,当我运行此代码时,它会给我这个错误吗?
public void setDataInList(int no) {
if (no == 0) {
ArrayList<Integer> count = helper.getRecordsCount(1);
mainGetLibraryModels.clear();
MainLibraryModelWS modelWS = helper.getAllRecordsMonthWise2(no);
for (int i = 0; i < modelWS.getRecords().size(); i++) {
WSLibraryModel model = new WSLibraryModel();
model.setAmount(modelWS.getRecords().get(i).getAmount());
model.setTotalCount("" + count.get(i));
model.setYearPart(modelWS.getRecords().get(i).getYearPart());
model.setMonthPart(modelWS.getRecords().get(i).getMonthPart());
mainGetLibraryModels.add(model);
}
adapter.notifyDataSetChanged();
} else if (no == 1) {
ArrayList<Integer> count = helper.getRecordsCount(2);
mainGetLibraryModels.clear();
MainLibraryModelWS modelWS = helper.getAllRecordsMonthWise2(no);
for (int i = 0; i < modelWS.getRecords().size(); i++) {
WSLibraryModel model = new WSLibraryModel();
model.setAmount(modelWS.getRecords().get(i).getAmount());
model.setTotalCount("" + count.get(i));
model.setYearPart(modelWS.getRecords().get(i).getYearPart());
model.setMonthPart(modelWS.getRecords().get(i).getMonthPart());
mainGetLibraryModels.add(model);
}
adapter.notifyDataSetChanged();
} else {
mainGetLibraryModels.clear();
MainLibraryModelWS modelWS = helper.getAllRecordsMonthWise2(no);
for (int i = 0; i < modelWS.getRecords().size(); i++) {
WSLibraryModel model = new WSLibraryModel();
model.setAmount(modelWS.getRecords().get(i).getAmount());
model.setTotalCount(" - ");
model.setYearPart(modelWS.getRecords().get(i).getYearPart());
model.setMonthPart(modelWS.getRecords().get(i).getMonthPart());
mainGetLibraryModels.add(model);
}
adapter.notifyDataSetChanged();
}
}
答案 0 :(得分:2)
而不是:
ArrayList<Integer> count = helper.getRecordsCount(1);
试试这个:
ArrayList<Integer> count = helper.getRecordsCount(0);
答案 1 :(得分:1)
首先,您需要避免多次调用getRecords()。它会降低性能并可能无法返回相同的结果 - 可能是您出错的原因。相反,引入一个本地参数作为此函数的缓存输出并使用它。
答案 2 :(得分:1)
首先使用switch
而不是if-else
分支,因为我从查询中看到你的结果会将结果的修正数作为数组给出(切换更有效并提高可读性)。< / p>
public void setDataInList(int no) { //just a suggestion not a answer
switch (no){
case 0 :
//Your Code as you specified in your code context.
break;
case 1 :
//Your Code as you specified in your code context.
break;
case 2 :
//Your Code as you specified in your code context.
break;
default :
break;
}
现在,如果您看到Exception
StackTrace并调试应用程序,一旦您轻松获得解决方案,就会出现问题。
这就是你的堆栈跟踪所说的 -
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
希望您知道这个受欢迎的异常IndexOutOfBoundsException
仅在您尝试访问数组中不存在的任何数组索引时出现。
现在,您的错误消息清楚地表明您的Array
尺寸为one
。这意味着您的数组只能访问索引zero
(0)。
所以,请调试你的代码,并尝试找出哪一行代码实际上产生了异常