【发布时间】:2022-12-13 22:25:17
【问题描述】:
我需要帮助按升序和降序对数组中的数字进行排序。偶数应该升序,奇数应该降序。
我已经设法按升序对数字进行排序,但想对奇数进行相反的操作。
实际结果:奇数和偶数都升序
预期结果:偶数递增,奇数递减
System.out.println("\n" + "random numbers generated:");
System.out.println(Arrays.toString(arrayList).replace("[", "").replace("]", "").replace(",", ""));
for (int i = 0; i < arrayList.length; i++) {
for (int j = i+1; j < arrayList.length; j++) {
if(arrayList[i] > arrayList[j]) {
temporaryArray = arrayList[i];
arrayList[i] = arrayList[j];
arrayList[j] = temporaryArray;
}
}
}
System.out.println("\n" + "random numbers arranged:");
int[] arrayTwo = Arrays.copyOf(arrayList, arrayList.length);
for (int i = 0; i < arrayList.length; i++) {
if(arrayTwo[i]%2!=0) {
System.out.print(arrayTwo[i] + " ");
}
}
System.out.print("| ");
for (int i = 0; i < arrayList.length; i++) {
if(arrayTwo[i]%2==0) {
System.out.print(arrayTwo[i] + " ");
}
}
如何反转奇数数组?
【问题讨论】: