import java.util.Random; public class Test { public static void main(String[] args) { //创建数组 int[] arr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; //遍历打乱数组内的数据 Random r = new Random(); int temp; for (int i = 0; i < arr.length; i++) { int index = r.nextInt(arr.length); temp = arr[i]; arr[i] = arr[index]; arr[index] = temp; } for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } //换行 System.out.println(); //定义二维数组 int[][] data = new int[4][4]; //给二维数组添加数字 //遍历二维数组 int index = 0; for (int i = 0; i < data.length; i++) { for (int j = 0; j < data.length; j++) { data[i][j] = arr[index]; index++; } } //遍历二维数组获取二维数组数据 for (int i = 0; i < data.length; i++) { for (int j = 0; j < data.length; j++) { System.out.print(data[i][j] + " "); } System.out.println(); } } }
运行结果