gpt4 book ai didi

java - Collections.sort 带有 2 个数组

转载 作者:行者123 更新时间:2023-12-02 04:45:37 25 4
gpt4 key购买 nike

我有 2 个 ArrayList。第一个保留第二个 ArrayList 中元素应如何排序的索引。如何使用 ArrayList 1 中的正确索引对 ArrayList 2 中的元素进行分组?

我的代码:

public void createRolette(Population population) throws Exception {
ArrayList<Integer> rouletteId = new ArrayList<Integer>();
ArrayList<Integer> rouletteFit = new ArrayList<Integer>();
for (int i=0; i<populationSize; i++) {
population.getIndividual(i);
Simulator.allocateTask(i);
rouletteId.add(i);
rouletteFit.add(calcFitness(i));
}
// Collections.sort(rouletteFit);

我的输出:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49] [90, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 90, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 88, 86, 88, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86, 86]

我正在尝试使用这个给定的解决方案,但代码中有一些我无法修复的错误。

public abstract class Roulette implements Comparable<Roulette>{

super(); //here says "Syntax error on token "super", Identifier expected"
int rouletteId;
int rouletteFit;

public Roulette(int rouletteId, int rouletteFit){
this.rouletteId = rouletteId;
this.rouletteFit = rouletteFit;
}

public int getId(){
return rouletteId;
}

public int getFit(){
return rouletteFit;
}

public static Comparator<Roulette> FitComparator = new Comparator<Roulette>() {

public int compare(Roulette r1, Roulette r2) {

int fit1 = r1.getFit();
int fit2 = r2.getFit();

//ascending order
return fit1.compareTo(fit2);

//descending order
//return fit2.compareTo(fit1);
}

};



public void createRoulette(Population population) throws Exception {
ArrayList<Roulette> rouletteList = new ArrayList<Roulette>();

for (int i=0; i<population.size(); i++){
population.getIndividual(i);
Simulator.allocateTask(i);
Roulette r = new Roulette(i, Simulator.calcFitness(i)); // here in "new Roulette says // - Multiple markers at this line
//- Cannot instantiate the type Roulette
//- Line breakpoint:Roulette [line: 48]

createRoulette(Population)
rouletteList.add(r);
}

Collections.sort(rouletteList, Roulette.FitComparator);
}
}

最佳答案

您还可以实现自己的排序算法并交换两个数组中的值。下面使用冒泡排序,一种简单的排序算法。

public static void createRolette(Population population) throws Exception {
ArrayList<Integer> rouletteId = new ArrayList<Integer>();
ArrayList<Integer> rouletteFit = new ArrayList<Integer>();
int swap;

for (int i=0; i<populationSize; i++){
population.getIndividual(i);
Simulator.allocateTask(i);
rouletteId.add(i);
rouletteFit.add(calcFitness(i));
}

//Bubble Sort
for (int i = 0; i < ( rouletteFit.size() - 1 ); i++) {
for (int j = 0; j < rouletteFit.size() - i - 1; j++) {
if (rouletteFit.get(j) > rouletteFit.get(j+1))
{
swap = rouletteFit.get(j);
rouletteFit.set(j, rouletteFit.get(j+1));
rouletteFit.set(j+1, swap);

swap = rouletteId.get(j);
rouletteId.set(j, rouletteId.get(j+1));
rouletteId.set(j+1, swap);
}
}
}
}

关于java - Collections.sort 带有 2 个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29683788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com