Collections.sort有2个数组

时间:2015-04-16 19:03:40

标签: java sorting arraylist

我有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);
}
}

3 个答案:

答案 0 :(得分:2)

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

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);
            }
        }
    }
}

答案 1 :(得分:0)

你可以使用ID和Fit创建一个对象的arraylist,或者你可以保留两个Arrays而不是两个arraylists的逻辑。

public class Roulette implements Comparable<Roulette>{
    super();
    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 - fit2);

      //descending order
      //return (fit2 -fit1);
    }

    };

}

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

    for (int i=0; i<populationSize; i++){
       population.getIndividual(i);
       Simulator.allocateTask(i);
       Roulette r = new Roulette(i, calcFitness(i));
       rouletteList.add(r)
    }

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

答案 2 :(得分:0)

使用您当前的ArrayLists实现,您不能简单地致电Collections.sort()对您的阵列列表进行分组,并且您的ID的映射也是相同的。要实现这一点,您基本上有3个选项:

  1. 创建自己的排序算法:您可以轻松实现一个简单的排序算法,该算法将rouletteFit的值分组,并重新组织rouletteId中的相应索引。
  2. 实施实施Class的自定义Comparable<ClassName>。这将允许您构建一个包含id和Fit在一个对象中的类,并允许您编写自定义compareTo()方法,因此您只需使用Collections.Sort()
  3. 创建地图:您可以使用HashMap类将拟合值存储为键,并使用ArrayList个索引来使用该拟合。定义看起来像HashMap<Integer, ArrayList<Integer>> map;