cp拷貝時檔案、目錄時強制覆蓋,不進行提示
阿新 • • 發佈:2020-12-01
# 稀疏陣列--一種資料結構(重點) 稀疏陣列的處理方式: 記錄陣列一共有幾行幾列,有多少個不同值 將具有不同值的元素和行列及值記錄在一個小規模的陣列中,從而縮小程式的規(壓縮) ```javascript public class SparseArrayDemo01 { public static void main(String\[\] args) { checkerboard(); } //稀疏陣列 public static void checkerboard(){ //建立一個11\*11的棋盤 0:沒有棋子 1:黑棋 2:白棋int\[\]\[\] checkerboard = new int\[11\]\[11\]; //在第一行第二列存:1 第二行第二列存:2 checkerboard\[0\]\[1\] = 1; checkerboard\[1\]\[1\] = 2; //列印棋盤 for (int\[\] c1:checkerboard){ for (int c2:c1){ System.out.print(c2+"\\t"); } System.out.println(); } System.out.println("列印棋盤--------------------"); //將棋盤中的有價值資料儲存到稀疏陣列中--(稀疏陣列的本質就是一個數組) //先獲取棋盤中的有價值資料有多少個--(這決定了稀疏陣列有多少行) int sum = 0; for (int\[\] ints : checkerboard) { for (int j = 0; j < checkerboard.length; j++) { if (ints\[j\] != 0) { sum++; } } } System.out.println("有價值的資料個數:"+sum); //先建立一個稀疏陣列 int\[\]\[\] sparse = new int\[sum+1\]\[3\]; //稀疏陣列的第一行儲存棋盤有多少行和列:行 列 有價值數的個數 sparse\[0\]\[0\] = 11; sparse\[0\]\[1\] = 11; sparse\[0\]\[2\] = sum; //將有價值數存入稀疏陣列中,按照:行 列 值;的方式儲存 int count=0; for (int i=0; i<checkerboard.length;i++) { for (int j = 0; j < checkerboard.length; j++) { if (checkerboard\[i\]\[j\] != 0) { count++; sparse\[count\]\[0\]=i; sparse\[count\]\[1\]=j; sparse\[count\]\[2\]=checkerboard\[i\]\[j\]; //System.out.println(checkerboard\[i\]\[j\]); } } } //列印稀疏陣列 for (int\[\] s1:sparse){ for (int s2:s1){ System.out.print(s2+"\\t"); } System.out.println(); } //還原棋盤 System.out.println("還原棋盤----------------"); //獲取稀疏陣列的第一行的資訊:行 列 個數 int row = sparse\[0\]\[0\]; int list = sparse\[0\]\[1\]; int\[\]\[\] checkerboard2 = new int\[row\]\[list\]; //再將資料寫入checkerboard2中 //int count2 = 1; //遍歷sparse for (int i = 1; i <sparse.length ; i++) { checkerboard2\[sparse\[i\]\[0\]\]\[sparse\[i\]\[1\]\] = sparse\[i\]\[2\]; //count2++; } for (int\[\] c1:checkerboard2){ for(int c2:c1){ System.out.print(c2+"\\t"); } System.out.println(); } } } 0 1 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 列印棋盤-------------------- 有價值的資料個數:2 11 11 2 0 1 1 1 1 2 還原棋盤---------------- 0 1 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ```