二维数组a的维度是6x7,请使用0~100的随机数完成二维数组a的初始化,然后将二维数组保存的随机数放置到一个一维数组b中,并进行降序排序,最后将b中排好序的随机数再复制回到二维数组a中用Java编写
时间: 2024-05-10 11:20:43 浏览: 90
```java
import java.util.Arrays;
import java.util.Random;
public class Main {
public static void main(String[] args) {
int[][] a = new int[6][7];
int[] b = new int[42];
// 初始化二维数组a
Random random = new Random();
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
a[i][j] = random.nextInt(101);
}
}
// 将二维数组a的随机数保存到一维数组b中
int index = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
b[index++] = a[i][j];
}
}
// 对一维数组b进行降序排序
Arrays.sort(b);
for (int i = 0; i < b.length / 2; i++) {
int temp = b[i];
b[i] = b[b.length - 1 - i];
b[b.length - 1 - i] = temp;
}
// 将排好序的随机数复制回二维数组a中
index = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
a[i][j] = b[index++];
}
}
// 打印二维数组a
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
```
阅读全文