package day02;
import java.util.Arrays;
import java.util.Random;
public class Test01 {
public static void main(String[] args) {
int[][]arr = new int[8][5];
Random r = new Random();
for (int m = 1; m < 100; m++) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 5; j++) {
arr[i][j]=r.nextInt(m);
}
}
}
//打印二維數組arr
printArrayA(arr);
int[]copy = new int[40];
//把二維數組轉到一維數組copy中
for(int i=0;i<8;i++){
System.arraycopy(arr[i], 0, copy, i*5, 5);
}
printArrayB(copy);
//給一維數組copy排序
Arrays.sort(copy);
//打印排序后的copy
printArrayB(copy);
//把一維數組copy轉到二維數組arr中
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 5; j++) {
System.arraycopy(copy, i*5, arr[i], 0, 5);
}
}
//打印排序后的arr數組
printArrayA(arr);
}
//打印arr數組
public static void printArrayA(int [][]arr){
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
//打印copy數組
public static void printArrayB(int []copy){
for (int i = 0; i < copy.length; i++) {
System.out.print(copy[i]+" ");
}
System.out.println();
}
}