JAVA程序。。。定义两个数组,首先把两个数组合并成一个数组,然后把新数组中的元素逆序排列

如题所述

public static void main(String[] args) {
Integer[] a = { 45, 9, 8 };
Integer[] b = { 3, 6, 5, 9 };
Integer[] result = new Integer[a.length + b.length];
System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, a.length, b.length);
//自定义排序
Comparator<Integer> comparator = new Comparator<Integer>() {

@Override
public int compare(Integer o1, Integer o2) {
// 如果n1小于n2,我们就返回正值,如果n1大于n2我们就返回负值,
// 这样颠倒一下,就可以实现反向排序了
if (o1 < o2) {
return 1;
} else if (o1 > o2) {
return -1;
} else {
return 0;
}
}
};
Arrays.sort(result,comparator);
System.out.println(Arrays.toString(result));

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-10-26
package com.dubo;
public class Sort {
public static void main(String[] args) {
int a[] = { 1, 5, 3 ,4};
int b[] = { 2, 9, 8 };
// 组合两个数组
int t[] = joinAndSort(a, b);
String result = "";
for (int i = 0; i < t.length; i++) {
result = result + t[i];
}
System.out.println(result);
}
private static int[] joinAndSort(int[] a, int[] b) {
int aLen = a.length, bLen = b.length;
int[] c = new int[aLen + bLen];
int[] t = new int[aLen + bLen];
for (int i = 0; i < aLen; i++) {
c[i] = a[i];
}
for (int j = 0; j < bLen; j++) {
c[j + aLen] = b[j];
}
for (int p = 0; p < c.length; p++) {
t[p] = c[c.length - p - 1];
}
return t;
}
}

求采纳
相似回答