交换两个数组的值,制作另一个没有重复的数组并从高到低排序

我知道我必须做一段时间或做 while 循环,但我无法让它工作。我也尝试使用 for 循环,但它总是给我一个错误,因为我不知道向量的确切长度,因为它们是随机的。


int a = (int)(Math.random() * 3 + 1);

int b = (int)(Math.random() * 3 + 1);

int c = a + b;

int[] arrA = new int[a];

int[] arrB = new int[b];

int[] arrC = new int[c];

for (int i = 0; i < a; i ++) {

    arrA[i] = (int)(Math.random() * 10 + 1);

    for (int j = 0; j < b; j ++) {

        arrB[j] = (int)(Math.random() * 10 + 1);

    }

}


Arrays.sort(arrA);

Arrays.sort(arrB);

System.out.println(Arrays.toString(arrA));

System.out.println(Arrays.toString(arrB));

System.out.println(Arrays.toString(arrC));


蓝山帝景
浏览 108回答 1
1回答

梵蒂冈之花

arrA从数组和中取值arrB,并插入到arrCint index = arrA.length;for (int i = 0; i < arrA.length; i++) {&nbsp; &nbsp; arrC[i] = arrA[i];}for (int i = 0; i < arrB.length; i++) {&nbsp; &nbsp; arrC[i + index] = arrB[i];&nbsp; &nbsp;&nbsp;}种类arrCArrays.sort(arrC);颠倒顺序并存储在arrDfor(int l = 0; l < arrC.length; l++) {&nbsp; &nbsp; arrD[l] = arrC[arrC.length - (l+1)];}删除重复项(简化)Set<Integer> remove=new LinkedHashSet<Integer>();for(int i = 0;i < arrD.length;i++){&nbsp; &nbsp; remove.add(arrD[i]);}删除重复项(通常)int index2 = 0;for (int i = 0; i < arrD.length; i++) {&nbsp; &nbsp; for (int k = 0; k < arrD.length; k++) {&nbsp; &nbsp; &nbsp; &nbsp; if (arrD[i] != arrD[k]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arrE[index2] = arrD[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index2++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java