java
package com.tay;标签:总结,tempArr,int,每日,++,length,数组,data From: https://www.cnblogs.com/tqylqt/p/17734560.html
import java.util.Random;
public class Test {
public static void main(String[] args) {
//把一个一维数组0-15打乱
//然后按照四个一组放到二维数组里
//1、定义一个一维数组
int[] tempArr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
//打乱数组中数据的顺序
Random r = new Random();
for (int i = 0; i < tempArr.length; i++) {
int index = r.nextInt(tempArr.length);
int temp = tempArr[i];
tempArr[i] = tempArr[index];
tempArr[index] = temp;
}
for (int i = 0; i < tempArr.length; i++) {
System.out.print(tempArr[i] + " ");
}
System.out.println();
//创建二维数组
int[][] data = new int[4][4];
//给二维数组添加数据
for (int i = 0; i < tempArr.length; i++) {
data[i / 4][i % 4] = tempArr[i];
}
//遍历二维数组
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
System.out.print(data[i][j] + " ");
}
System.out.println();
}
}
}