我有这个代码: indexOfChromosomes:48 indexOfGens:20 双截断; 双交叉; 双重突变; int Generation;
private Context context;
int indexOfChromosomes;
int indexOfGens;
int gensNumber;
int chromosomesNumber;
public AdapterDB(int Bits, double Truncation, double Crossover, double Mutation, int Chromosomes, int Generation, Context ctx)
{
this.indexOfGens = Bits;
this.Truncation = Truncation;
this.Crossover = Crossover;
this.Mutation = Mutation;
this.indexOfChromosomes = Chromosomes;
this.Generation = Generation;
this.context = ctx;
DBHelper = new DatabaseHelper (context);
}
String [][] population = new String[indexOfChromosomes][indexOfGens];
public void generateChromosome()
{
for(chromosomesNumber = 0; chromosomesNumber < indexOfChromosomes ; chromosomesNumber++)
{
int o = 0;
for(gensNumber = 0; gensNumber < 12 ; gensNumber++)
{
Cursor l = 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 AND (key_carbohydrate!=0 OR key_protein!=0 OR key_fat!=0) ORDER BY RANDOM() LIMIT 1", null);
if((l.moveToFirst()) && (l!=null))
{
if (o == indexOfGens)
{
gensNumber = 0;
sumOfCarbohydrateMor = 0;
sumOfFatMor = 0;
sumOfProteinMor = 0;
sumOfCalorieMor = 0;
o = 0;
}
population[chromosomesNumber][gensNumber] = l.getString(0);
morning_food[k] = l.getString(3);
sumOfCarbohydrateMor = sumOfCarbohydrateMor + Double.parseDouble(morning_food[k]);
morning_food[f] = l.getString(4);
sumOfFatMor = sumOfFatMor + Double.parseDouble(morning_food[f]);
morning_food[p] = l.getString(5);
sumOfProteinMor = sumOfProteinMor + Double.parseDouble(morning_food[p]);
morning_food[c] = l.getString(2);
sumOfCalorieMor = sumOfCalorieMor + Double.parseDouble(morning_food[c]);
if (((sumOfCarbohydrateMor >= (morning_car-(morning_car*0.2))) && (sumOfCarbohydrateMor <= morning_cal*1.1)) && ((sumOfProteinMor >= (morning_pro-(morning_pro*0.2))) && (sumOfProteinMor <= morning_pro*1.1)) && ((sumOfFatMor >= (morning_fat-(morning_fat*0.2))) && (sumOfFatMor <= morning_fat*1.1)))
//if((sumOfCarbohydrateMor > (morning_car*0.6)) && (sumOfProteinMor > (morning_pro*0.7)) && (sumOfFatMor > (morning_fat*0.8)))
{
Log.e("lala", "lalala");
break;
}
if ((sumOfCarbohydrateMor > (morning_car*1.1)) || (sumOfProteinMor > (morning_pro*1.1)) || (sumOfFatMor > (morning_fat*1.1)) || (sumOfCalorieMor > (morning_cal*1.1))
{
morning_food[k] = l.getString(3);
sumOfCarbohydrateMor = sumOfCarbohydrateMor - Double.parseDouble(morning_food[k]);
morning_food[f] = l.getString(4);
sumOfFatMor = sumOfFatMor - Double.parseDouble(morning_food[f]);
morning_food[p] = l.getString(5);
sumOfProteinMor = sumOfProteinMor - Double.parseDouble(morning_food[p]);
morning_food[c] = l.getString(2);
sumOfCalorieMor = sumOfCalorieMor - Double.parseDouble(morning_food[c]);
gensNumber--;
o++;
}
}
}
并且它出错:48
错误说明:java.lang.ArrayIndexOutOfBoundsException
任何想法?你好
答案 0 :(得分:1)
这是你的问题:
String [][] population = new String[indexOfChromosomes][indexOfGens];
这出现在你的构造函数之外,因此在你的构造函数之前执行,当indexOfChromosomes
和indexOfGens
都是0时。初始化里面你的构造函数。这是一个更简单的示例,显示了同样的问题:
public class Test {
private int size;
public Test(int size) {
this.size = size;
}
private String[] array = new String[size];
public static void main(String[] args) {
Test t = new Test(5);
System.out.println(t.array.length);
}
}
固定版本:
public class Test {
private int size;
private String[] array;
public Test(int size) {
this.size = size;
array = new String[size];
}
public static void main(String[] args) {
Test t = new Test(5);
System.out.println(t.array.length);
}
}
请注意,变量声明相对于构造函数的定位对执行流没有影响。
编辑:至于为什么它现在永远循环 - 在你的代码中间你有:
if (o == indexOfGens)
{
gensNumber = 0;
...
}
将内循环重置回(几乎)开始(不完全是开始,因为gensNumber
将在循环体的末尾递增,在下一次迭代开始之前)。
一点也不清楚你要做什么,但我怀疑这没有帮助。
我还鼓励你尽可能使用本地变量 - 例如,非常使用实例变量作为循环计数器。
最后,我建议您将大型方法拆分为较小的方法以便于阅读。
答案 1 :(得分:1)
尝试设置
population = new String[indexOfChromosomes][indexOfGens];
在adapterDB中。
看起来初始化populatioin时,indexOfChromosomes和indexOfGens尚未初始化,所以你要创建一个大小为0的数组。所以当你调用时
population[chromosomesNumber][gensNumber]
你得到java.lang.ArrayIndexOutOfBoundsException
答案 2 :(得分:1)
这是因为您在函数外部定义了种群字符串,而没有将indexOfChromosomes初始化为48并且indexOfGens为20.尝试在设置变量之后在顶部定义种群并将其设置为适配器中的新内容。像这样:
double Truncation;
double Crossover;
double Mutation;
int Generation;
private Context context;
int indexOfChromosomes;
int indexOfGens;
int gensNumber;
int chromosomesNumber;
String [][] population;
public AdapterDB(int Bits, double Truncation, double Crossover, double Mutation, int Chromosomes, int Generation, Context ctx)
{
this.indexOfGens = Bits;
this.Truncation = Truncation;
this.Crossover = Crossover;
this.Mutation = Mutation;
this.indexOfChromosomes = Chromosomes;
this.Generation = Generation;
this.context = ctx;
DBHelper = new DatabaseHelper (context);
//Create population after initializing variables.
population = new String[indexOfChromosomes][indexOfGens];
}