跳到主要内容

02、数据结构与算法 - 基础:稀疏数组

假设创建 11 行 11 列的棋盘
0:无子 1:黑子 2:白子
将棋盘内的棋子使用稀疏队列持久化后再读取恢复存档

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;

@SpringBootTest
class SparsearrayApplicationTests {
   
     

   Logger log = LoggerFactory.getLogger(SparsearrayApplicationTests.class);
   // 文件存储地址
   public static final String FILE_PATH = "E:\\sparseArray.data";

   @Test
   void createSparseArray() throws FileNotFoundException {
   
     
       // 创建 11 行 11 列的棋盘
       // 0:无子 1:黑子 2:白子
       int [][] arr = new int[11][11];
       arr[1][3] = 1;
       arr[2][4] = 2;
       arr[2][2] = 1;

       System.out.println("-----遍历输出原始棋盘-----");
       int sum = 0; // 记录棋盘中非零数据个数
       for (int i = 0; i < arr.length; i++) {
   
     
           for (int j = 0; j < arr[i].length; j++) {
   
     
               if (arr[i][j] != 0) {
   
     
                   sum++;
               }
               System.out.printf("%d\t", arr[i][j]);
           }
           System.out.println();
       }

       // 将非 0 的数据存入 sparseArray 中
       // 给稀疏数组赋值 第一行为:行数 列数 值
       int[][] sparseArray = new int[sum + 1][3];
       sparseArray[0][0] = arr.length;
       sparseArray[0][1] = arr[0].length;
       sparseArray[0][2] = sum;
       int count = 0; // 计算为第几个非 0 数据
       // 将二维数组内存在不为零的数据存入稀疏数组
       for (int i = 0; i < arr.length; i++) {
   
     
           for (int j = 0; j < arr[i].length; j++) {
   
     
               if (arr[i][j] != 0) {
   
     
                   count ++;
                   sparseArray[count][0] = i;
                   sparseArray[count][1] = j;
                   sparseArray[count][2] = arr[i][j];
               }
           }
       }
       log.info("-----遍历输出稀疏数组-----");
       for (int i = 0; i < sparseArray.length; i++) {
   
     
           for (int j = 0; j < sparseArray[i].length; j++) {
   
     
               System.out.printf("%d\t", sparseArray[i][j]);
           }
           System.out.println();
       }

       // 存储棋盘
       saveSparseArray(sparseArray, new File(FILE_PATH));

       log.info("-----恢复存档棋盘数据-----");
       // 读取数据返回存档棋盘数据
       int[][] newArray = readSparseArray(sparseArray, new FileReader(FILE_PATH));
       if (newArray != null) {
   
     
           for (int i = 0; i < newArray.length; i++) {
   
     
               for (int j = 0; j < newArray[i].length; j++) {
   
     
                   System.out.printf("%d\t", newArray[i][j]);
               }
               System.out.println();
           }
       }
   }

   /**
    * @Desc    将数组存储到文件中
    * @Param   array   数组
    * @Param   file    存储文件
    * @Return
    */
   public static void saveSparseArray(int[][] array, File file){
   
     
       try (FileWriter outputStream = new FileWriter(file)) {
   
     
           if (file.exists()) {
   
     
               file.createNewFile();
               for (int i = 0; i < array.length; i++) {
   
     
                   for (int j = 0; j < array[i].length; j++) {
   
     
                       outputStream.write(array[i][j] + "\t");
                   }
                   outputStream.write("\n");
               }
           }
       } catch (Exception e) {
   
     
           e.printStackTrace();
       }
   }

   public static int[][] readSparseArray(int[][] array, FileReader file) {
   
     
       try (BufferedReader reader = new BufferedReader(file)) {
   
     
           String line = null;
           String[] arr = null;
           int[][] newArray = null;
           int num = 0;
           // 遍历读取文件内数据
           while ((line = reader.readLine()) != null) {
   
     
               arr = line.split("\t");
               if (num == 0) {
   
     
                   newArray = new int[Integer.valueOf(arr[2]) + 1][3];
                   for (int i = 0; i < arr.length; i++) {
   
     
                       newArray[num][i] = Integer.valueOf(arr[i]);
                   }
                   num++;
                   continue;
               }
               for (int i = 0; i < arr.length; i++) {
   
     
                   newArray[num][i] = Integer.valueOf(arr[i]);
               }
               num++;
           }
           array = new int[newArray[0][0]][newArray[0][1]];
           for (int i = 1; i < newArray.length; i++) {
   
     
               array[newArray[i][0]][newArray[i][1]] = newArray[i][2];
           }
           return array;
       } catch (Exception e) {
   
     
           e.printStackTrace();
       }
       return null;
   }
}