我一直试图通过我提供的代码对Double类型的二维数组进行排序,我的数组不包含任何0,但在排序后有几个0。
排序代码
Arrays.sort(merge, new Comparator<double[]>() {
@Override
public int compare(double[] o1, double[] o2) {
return Double.compare(o1[4], o2[4]);
}
});
这是我的完整代码,我想根据数组[i] [4]进行排序 即第四指数 : -
public class EvoAlgo {
static int k = 40;//generations
static double al = 0, ah = 1, bl = -2, bh = 2, cl = -2, ch = 1, dl = 0.5, dh = 1;
static double m1 = 0.02, m2 = 0.22, m3 = -0.11, m4 = 0.22;
static double f(double a, double b, double c, double d) {
return (2 * a * a) - (2.5 * a * b * c * c) + (4 * b * c * d) + (0.25 * c * d) - (0.2 * d * d);
}
static double[] mutate(double[] child) {
double m = Math.random();
if (m <= 0.25) {
child[0] = child[0] * m1;
} else if (m <= 0.5) {
child[1] = child[1] * m2;
} else if (m <= 0.75) {
child[2] = child[2] * m3;
} else {
child[3] = child[3] * m4;
}
child[4] = f(child[0], child[1], child[2], child[3]);
return child;
}
static double[][] initRandomPop() {
double[][] initPop = new double[25][5];
for (int i = 0; i < initPop.length; i++) {
double ax = al + (Math.random() * ((ah - al) + 1));
double bx = bl + (Math.random() * ((bh - bl) + 1));
double cx = cl + (Math.random() * ((ch - cl) + 1));
double dx = dl + (Math.random() * ((dh - dl) + 1));
initPop[i][0] = ax;
initPop[i][1] = bx;
initPop[i][2] = cx;
initPop[i][3] = dx;
initPop[i][4] = f(ax, bx, cx, dx);
}
return initPop;
}
static double[][] merge(double[][] a, double[][] b) {
double[][] merge = new double[a.length + b.length][5];
for (int i = 0; i < a.length; i++) {
merge[i] = a[i];
}
for (int i = a.length; i < b.length; i++) {
merge[i] = a[i];
}
return merge;
}
static double[][] newPop(double[][] a) {
double[][] newPop = new double[25][5];
for (int i = 0; i < 25; i++) {
newPop[i] = a[i];
}
return newPop;
}
public static void main(String[] args) {
double[][] initPop = initRandomPop();
double[][] child = new double[40][5];
int cc = 0;
for (int i = 0; i < k; i++) {
for (int j = 0; j < 20; j++) {
int ri1 = 0 + (int) (Math.random() * ((24 - 0) + 1));
int ri2 = 0 + (int) (Math.random() * ((24 - 0) + 1));
double[] ind1 = initPop[ri1];
double[] ind2 = initPop[ri2];
double[] c1 = new double[5];
double[] c2 = new double[5];
c1[0] = ind1[0];
c1[1] = ind1[1];
c1[2] = ind2[2];
c1[3] = ind2[3];
c1[4] = f(ind1[0], ind1[1], ind2[2], ind2[3]);
c2[0] = ind2[0];
c2[1] = ind2[1];
c2[2] = ind1[2];
c2[3] = ind1[3];
c2[4] = f(ind2[0], ind2[1], ind1[2], ind1[3]);
c1 = mutate(c1);
c2 = mutate(c2);
child[cc++] = c1;
child[cc++] = c2;
}
double[][] merge = merge(child, initPop);
Arrays.sort(merge, new Comparator<double[]>() {
@Override
public int compare(double[] o1, double[] o2) {
return Double.compare(o1[4], o2[4]);
}
});
for (int j = 0; j < merge.length; j++) {
System.out.println(merge[j][4]);
}
initPop = newPop(merge);
cc = 0;
}
System.out.println("Fittest Person On Earth " + initPop[0][4]);
}
}
答案 0 :(得分:0)
0是默认值。它可以与null进行比较,因此很可能您会遇到数组字段未赋值的情况。
答案 1 :(得分:0)
抱歉错误的问题!问题出在merge()方法中。 这是修复
static double[][] merge(double[][] a, double[][] b) {
double[][] merge = new double[a.length + b.length][5];
for (int i = 0; i < a.length; i++) {
merge[i] = a[i];
}
int p = 0;
for (int i = a.length; i < merge.length; i++) {
merge[i] = b[p++];
}
return merge;
}