feat(class14): 提交了自己写的一坨屎山,塞了一堆语法糖....
糖很好吃捏🤗
- 新增 ArrayDemo5 类,演示二维数组的定义和遍历
- 新增 ArrayTest6 类,实现二维数组的初始化、打印和随机打乱功能
This commit is contained in:
21
src/class14/ArrayDemo5.java
Normal file
21
src/class14/ArrayDemo5.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package class14;
|
||||
|
||||
public class ArrayDemo5 {
|
||||
public static void main(String[] args) {
|
||||
printArray();
|
||||
}
|
||||
public static void printArray(){
|
||||
String[][] classroom={
|
||||
{"张无极","张三","张三"},
|
||||
{"张无极","张三","张三"},
|
||||
{"张无极","张三","张三"}
|
||||
};
|
||||
for(String[] row:classroom){
|
||||
for(String name:row){
|
||||
System.out.println(name);
|
||||
}
|
||||
}
|
||||
int [][] arr= new int[5][5];
|
||||
System.out.println(arr.length);
|
||||
}
|
||||
}
|
||||
40
src/class14/ArrayTest6.java
Normal file
40
src/class14/ArrayTest6.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package class14;
|
||||
|
||||
public class ArrayTest6 {
|
||||
public static void main(String[] args) {
|
||||
start(5);
|
||||
|
||||
}
|
||||
public static void start(int n){
|
||||
int [][] arr= new int[n][n];
|
||||
int count = 0;
|
||||
for(int i=0; i< arr.length; i++){
|
||||
for(int j=0; j< arr.length; j++){
|
||||
arr[i][j]=count++;
|
||||
}
|
||||
}
|
||||
printArray(arr);
|
||||
System.out.println("=================");
|
||||
printArray(shuffle(arr));
|
||||
}
|
||||
public static void printArray(int[][] arr){
|
||||
for(int[] row: arr){
|
||||
for(int data: row){
|
||||
System.out.print(data+" ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
//二维数组打乱方法
|
||||
public static int[][] shuffle(int[][] arr){
|
||||
for(int i=0; i< arr.length; i++){
|
||||
for(int j=0; j< arr.length; j++){
|
||||
double[] index = {Math.random() * arr.length, Math.random() * arr.length};
|
||||
int temp = arr[i][j];
|
||||
arr[i][j] = arr[(int)index[0]][(int)index[1]];
|
||||
arr[(int)index[0]][(int)index[1]] = temp;
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user