我有这个 :

String [][] population = new String[indexOfChromosomes][indexOfGens];

for(int chromosomesNumber = 0; chromosomesNumber <= indexOfChromosomes ; chromosomesNumber++){
  for(int gensNumber = 0; gensNumber <= indexOfGens ; gensNumber++){
    Cursor r = db.rawQuery("SELECT _id, key_foodstuff, key_calorie, key_carbohydrate, key_fat,
      key_protein FROM (food INNER JOIN categories ON food.key_nocategory =
      categories.nocategories) WHERE key_type='primary'
      AND _id!=164 ORDER BY RANDOM() LIMIT 1", null);

      if(r.moveToFirst()){
       population[indexOfChromosomes][indexOfGens] = r.getString(0);
      }
   }
}


运行此代码时出现错误:
人口[indexOfChromosomes] [indexOfGens] = r.getString(0);

我想问一下,如何将光标r放在数组2维上?谢谢

最佳答案

看起来错误在这里:

String [][] population = new String[indexOfChromosomes][indexOfGens];

for(int chromosomesNumber = 0; chromosomesNumber <= indexOfChromosomes ; chromosomesNumber++){
    for(int gensNumber = 0; gensNumber <= indexOfGens ; gensNumber++){


数组的大小为indexOfChromosomes x indexOfGens,因此,当您尝试访问索引0..indexOfChromosomes-10..indexOfGens-1时,它的索引为indexOfChromosomesindexOfGens

它应该是

for(int chromosomesNumber = 0; chromosomesNumber < indexOfChromosomes ; chromosomesNumber++){
    for(int gensNumber = 0; gensNumber < indexOfGens ; gensNumber++){

08-27 14:16