我需要排序一个二维arrylist Java和获取排序元素的索引。要做到这一点我 1.第一次写这个代码,我做一个普通类数组元素进行排序,并获得排序元素的原始指数:排序2d arraylist和得到索引java
public static int[] Sort_Index(double[] arr){
int[] indices = new int[arr.length];
indices[0] = 0;
for(int i=1;i<arr.length;i++){
int j=i;
for(;j>=1 && arr[j]<arr[j-1];j--){
double temp = arr[j];
arr[j] = arr[j-1];
indices[j]=indices[j-1];
arr[j-1] = temp;
}
indices[j]=i;
}
return indices;//indices of sorted elements
}
然后我用这个循环来安排的ArrayListÿ
for(int i=0;i<Input.General_Inputs.Num_objectives;i++){
double[] sort_y=new double[y.size()];
for(int row=0;row<y.size();row++)
sort_y[row]=y.get(row).get(Input.General_Inputs.Num+i);
int[] sort_y_index=Sort_Index(sort_y);
}
}
对我来说,下一步就是使用这个索引将y ArrayList中的值存储到新的ArrayList中。但我认为这是完全没有效率的更好的想法?
感谢您的回复,但这个概念对我来说是新的,你可以举一个简单的例子来说明如何使用这个概念谢谢 – 2015-02-11 22:39:15