稀疏数组

1、需求场景

  • 当二维数组的大部分值是默认值0的时候,可以考虑使用稀疏数组,譬如:

2、二维数组转换为稀疏数组

思路分析

  • 记录数组有m行n列,有p个非零值--->对应到稀疏数组的第0行(遍历原始数组
  • 在稀疏数组中,记录原数组的非零值的行列坐标(x, y)和数值v---> 从第一行分别往下列出

3、稀疏数组还原为二维数组

  • 由稀疏数组的第一行----->得到原始数组的行列数,就可以确定数组的大小
  • 读取剩余的行,就可以填充原始数组。

4、例题实现

  • 将二维数组转换为稀疏数组,然后存储到磁盘,实现棋盘的保存功能;
  • 读取磁盘文件,恢复成为原来的棋盘,实现续上盘的功能;

  • 代码实现
public class ExerciseArr {
    public static void main(String[] args) {
        int[][] chessArray = initArray();

        int[][] sparseArray = ArrayToSparseArray(chessArray);

        storeToDisk(sparseArray);

        int[][] readFromDisk = readFromDisk();
        for (int[] row : readFromDisk) {
            for (int i : row) {
                System.out.print(i + " ");
            }
            System.out.println();
        }
    }

    /**
     * 初始化一个二维数组(代表前盘)
     * @return 初始化的数组
     */
    public static int[][] initArray(){
        int[][] chessArr = new int[11][11];
        chessArr[1][2] = 1;
        chessArr[2][3] = 2;

        return chessArr;
    }


    /**
     * 棋盘数组--->稀疏数组
     * @param chessArray 棋盘数组
     * @return 稀疏数组
     */
    public static int[][] ArrayToSparseArray(int[][] chessArray){
        //遍历棋盘数组,统计非零元素个数
        int sum = 0;
        for (int[] row : chessArray) {
            for (int data : row) {
                if (data != 0)
                    sum++;
            }
        }

        //创建稀疏数组,填充第一行
        int[][] sparseArray = new int[sum+1][3];
        sparseArray[0][0] = 11;
        sparseArray[0][1] = 11;
        sparseArray[0][2] = sum;

        //稀疏数组,填充其余行
        int sparseRow = 1;
        for (int i = 0; i < chessArray.length; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArray[i][j] != 0){
                    sparseArray[sparseRow][0] = i;
                    sparseArray[sparseRow][1] = j;
                    sparseArray[sparseRow++][2] = chessArray[i][j];
                }
            }
        }

        return sparseArray;
    }


    /**
     * 稀疏数组--->棋盘数组
     * @param sparseArray 稀疏数组
     * @return 棋盘数组
     */
    public static int[][] sparseArrayToArray(int[][] sparseArray){
        int[][] array = new int[sparseArray[0][0]][sparseArray[0][1]];

        for (int i = 1; i <sparseArray.length; i++) {
            array[sparseArray[i][0]][sparseArray[i][1]]=sparseArray[i][2];
        }

        return array;
    }


    /**
     * 用数据流DataOutputStream将数据写出到硬盘
     * @param sparseArray 稀疏数组
     */
    public static void storeToDisk(int[][] sparseArray) {
        //造流
        DataOutputStream dos = null;
        try {
            dos = new DataOutputStream(new FileOutputStream(new File("storeArray.txt")));
            //遍历数组,写入文件
            for (int[] row : sparseArray) {
                for (int data : row) {
                    dos.writeInt(data);
                }
            }
            dos.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (dos != null) {
                    dos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static int[][] readFromDisk() {
        DataInputStream dis = null;
        int[][] sparseArray = new int[0][];
        try {
            dis = new DataInputStream(new FileInputStream("storeArray.txt"));

            sparseArray = new int[3][3];
            for (int i = 0; i < sparseArray.length; i++) {
                for (int j = 0; j < sparseArray[0].length; j++) {
                    sparseArray[i][j] = dis.readInt();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (dis != null) {
                    dis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return sparseArray;
    }
}
posted @ 2020-12-21 20:17  不知老之将至  阅读(119)  评论(0编辑  收藏  举报