是在學習尚硅谷的數據結構與算法Java課程,課后自己憑借思路寫的練習代碼
首先定義一個稀疏數組類
import java.io.*;
import java.util.Objects;public class SparseArray {int sum;//創建原始數組public int[][] createArray(int column, int row){//根據傳入數據定義原始數組大小int[][] array = new int[row][column];//設置一些初始值int position1_row = 1;int position1_column = 2;int position1_value = 1;int position2_row = 3;int position2_column = 4;int position2_value = 2;//賦值初始值,默認值為0, 1表示數據1,2表示數據2System.out.println("生成初始數組如下:");for (int i = 0; i < array.length; i++) {for (int j = 0; j < column; j++) {if (i == position1_row && j == position1_column){array[i][j] = position1_value;}else if (i == position2_row && j == position2_column){array[i][j] = position2_value;}else {array[i][j] = 0;}System.out.printf("%d\t",array[i][j]);}System.out.println();}return array;}//根據原始數組創建稀疏數組public int[][] builtSparseArray(int[][] originalArray){int row = originalArray.length;int column = originalArray[0].length;int sum = 0;for (int i = 0; i < row; i++) {for (int data : originalArray[i]) {if (data != 0){sum++;}}}int[][] sparseArray = new int[sum+1][3];sparseArray[0][0] = row;sparseArray[0][1] = column;sparseArray[0][2] = sum;int count = 1;System.out.println("生成稀疏數組如下");System.out.printf("%d\t%d\t%d\t\n",row,column,sum);for (int i = 0; i < row; i++) {for (int j = 0; j < column; j++) {if (originalArray[i][j] != 0){sparseArray[count][0] = i;sparseArray[count][1] = j;sparseArray[count][2] = originalArray[i][j];System.out.printf("%d\t%d\t%d\t\n",i,j,originalArray[i][j]);count++;}}}return sparseArray;}//根據稀疏數組恢復原始數組public int[][] restoreArray(int[][] sparseArray){int[][] restoredArray = new int[sparseArray[0][0]][sparseArray[0][1]];for (int i = 0; i < sparseArray[0][0]; i++) {for (int j = 0; j < sparseArray[0][1]; j++) {restoredArray[i][j] = 0;}}for(int i = 1; i < sparseArray.length; i++){restoredArray[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];}System.out.println("復原數組如下:");for (int i = 0; i < sparseArray[0][0]; i++) {for (int j = 0; j < sparseArray[0][1]; j++) {System.out.printf("%d\t",restoredArray[i][j]);}System.out.println();}return restoredArray;}public void saveArray(int[][] array) {BufferedWriter writer = null;try {writer = new BufferedWriter(new FileWriter("sparseArray.txt"));for (int i = 0; i < array.length; i++) {for (int j = 0; j < array[0].length; j++) {int data = array[i][j];writer.write(data+",");}writer.newLine();}writer.close();System.out.println("稀疏數組保存成功");} catch (IOException e) {e.printStackTrace();}}public int[][] readArray(){int [][] sparseArray = null;try {String data;BufferedReader reader = new BufferedReader(new FileReader("sparseArray.txt"));boolean flag = true;int count = 0;System.out.println("讀到的稀疏數組如下:");while ((data = reader.readLine()) != null){String[] splits = data.split(",");if (flag){sum = Integer.parseInt(splits[2]);sparseArray = new int[sum+1][3];}flag = false;sparseArray[count][0] = Integer.parseInt(splits[0]);sparseArray[count][1] = Integer.parseInt(splits[1]);sparseArray[count][2] = Integer.parseInt(splits[2]);System.out.printf("%d\t%d\t%d\t\n",sparseArray[count][0],sparseArray[count][1],sparseArray[count][2]);count++;}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return (Objects.isNull(sparseArray))?null:sparseArray;}
}
然后主類代碼
public class Start {public static void main(String[] args) {//定義稀疏數組類對象SparseArray sparseArray = new SparseArray();//創建原始數組int[][] array = sparseArray.createArray(11, 11);//創建稀疏數組int[][] sparseArrayInstance = sparseArray.builtSparseArray(array);//保存稀疏數組sparseArray.saveArray(sparseArrayInstance);//讀取稀疏數組int[][] ints = sparseArray.readArray();//恢復原始數組sparseArray.restoreArray(sparseArrayInstance);}
}
打印結果,存儲是存到txt文件中
生成初始數組如下:
0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 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
生成稀疏數組如下
11 11 2
1 2 1
3 4 2
稀疏數組保存成功
讀到的稀疏數組如下:
11 11 2
1 2 1
3 4 2
復原數組如下:
0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 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 Process finished with exit code 0