使用一些计算智能算法进行规划,这对我来说是一个新领域。
由于一些奇怪的原因,我在找到特定数组的索引时遇到了麻烦。
例如,我有两个数组,parentOne和parentTwo都是Integer类型。我将3个随机整数值添加到1到300之间的两个数组列表中。
然后我尝试从1-300之间的数组中随机选择的索引中检索值,例如,选择city may = 129,然后代码应转到parentOne的索引129并返回相关值但是,它返回-1。
奇特之处在于,当查找任何索引<35时,这完全有效。
非常感谢任何帮助!
谢谢。
private int[][] cycleCrossover(int first, int second){
//tempArray to return
int[][] tempArray = new int[2][numberOfAreas];
if(cycleCrossover == true){
//select a random city
int selectedCity = 35;//random.nextInt(numberOfAreas);
int parentOneCity = 0;
int parentTwoIndex = 0;
int parentTwoCity = 0;
int parentOneIndex = 0;
int index = 0;
//used to store parent and exchange values as well as cycle
ArrayList<Integer> parentOne = new ArrayList<Integer>();
ArrayList<Integer> parentTwo = new ArrayList<Integer>();
ArrayList<Integer> cycle = new ArrayList<Integer>();
//Assign values to the arrays
for(int i = 0; i <numberOfAreas; i++){
parentOne.add(population[first][i]);
parentTwo.add(population[second][i]);
}
System.out.println(parentOne);
//get parentOne Cities into new array
long count = selectedCity;
//Determine my cycle contents
while(count > 0){
//Get the index of the first city
index = parentOne.indexOf(selectedCity);
//Add the first cities index to my cycle
cycle.add(parentOne.indexOf(selectedCity));
//add the next city to my parents cycle
selectedCity = parentTwo.get(index);
//take 1 from count
count = count - 1;
}
}
答案 0 :(得分:1)
如果ArrayList的indexOf
方法返回-1,则表示您在ArrayList中搜索的对象不存在。