已经为此工作了几个小时,所以我想我会问,当我创建这个对象的数组时,给定随机输入的内存数组会导致数组的其余部分具有相同的输出:请参见下面的代码。
蜜蜂类:
private class Bee{
private int status;
private float[] memory;
private double quality;
private int visits;
private Bee(int status, float[] memory, double quality, int visits){
this.status = status;
this.memory = memory;
this.quality = quality;
this.visits = visits;
}
}
打印时单个蜜蜂的质量不同,但它们的记忆保持不变。
for(int i = 0; i < this.bees.length; i++){
System.out.println(this.bees[i].memory[0]+" "+this.bees[i].memory[1]);
System.out.println(this.bees[i].quality);
}
样本输出:
3.4968524 8.354554
1581.5435920638447
3.4968524 8.354554
82.46318172154176
3.4968524 8.354554
66.25267691464408
我不明白为什么质量有效但内存无效?
任何帮助或建议将不胜感激。
编辑:
for(int i = 0; i < totalNumberbees; i++){
int beestatus;
if(i < numberInactive){
beestatus = 0;//inactive
indexesOfInactivebees[i] = i;
} else if(i < (numberInactive + numberScout)){
beestatus = 2;//scout
} else{
beestatus = 1;//active
}
float[] randomMemory = GenerateRandomMemory();
this.bees[i] = new Bee(beestatus, randomMemory, MeasureQuality(randomMemory), 0);
}
private float[] GenerateRandomMemory(){
float[] result = new float[this.functionData.vars.length];
result = this.functionData.vars;
for(int i = 0; i < result.length; i++){
float r1 = new Random().nextFloat()*10;
result[i] = r1;
}
return result;
}
这是 GenerateRandomClass 和初始化蜜蜂的其余代码。