数据结构与算法之稀疏数组(棋盘的保存和续局)

稀疏数组: 
Int[][] sparseArray = new Int[原始数组有效值+1][3]
// 行                       列                 值
// 源数组总行数              源数组总列数         有几个有意义的值
// 第几行(从0开始)           第几列(从0开始)      值是什么

 

为什么会用到稀疏数组: 如果原始的二维数组很多数据没有意义, 可以用到稀疏数组来达到数据压缩、节省空间的目的
稀疏数组的应用:

public class SparseArrayTest {
    public static void main(String[] args) {
        int[][] chessArray = new int[11][11];
        chessArray[0][0] = 1;
        chessArray[3][5] = 2;
        chessArray[4][6] = 3;
        getChess(chessArray, "F:\\data-structure\\exercise.data");
    }

    // 当前有一个11 x 11 的棋盘, 上面有未下完的棋子, 请实现保存棋盘和再续上局的功能
    public static void getChess(int[][] chessArray, String pathname) {
        // ====================== 保存棋盘: start =====================
        // 1. 判断原始数组的棋子个数
        int chessNum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArray[i][j] != 0) {
                    chessNum++;
                }
            }
        }
        // 2. 创建稀疏数组
        int[][] sparseArray1 = new int[chessNum + 1][3];
        // 3. 构造稀疏数组
        sparseArray1[0][0] = 11;
        sparseArray1[0][1] = 11;
        sparseArray1[0][2] = chessNum;
        int row = 1;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArray[i][j] != 0) {
                    sparseArray1[row][0] = i;
                    sparseArray1[row][1] = j;
                    sparseArray1[row][2] = chessArray[i][j];
                    row++;
                }
            }
        }
        // 遍历打印稀疏数组
        System.out.println("打印转换后的稀疏数组: ");
        for (int[] ints : sparseArray1) {
            System.out.printf("%d\t%d\t%d\n", ints[0], ints[1], ints[2]);
        }
        // 通过IO流保存稀疏数组
        try (FileWriter fw = new FileWriter(new File(pathname))) {
            for (int[] ints : sparseArray1) {
                fw.write(ints[0] + "\t"); // 注意: 这里要写入字符串, 不然自动转换成ASCII值了
                fw.write(ints[1] + "\t");
                fw.write(ints[2] + "");
                fw.write("\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // ====================== 保存棋盘: end =======================

        // ====================== 继续上局: start =====================
        // 1. 通过IO流读取稀疏数组, 拿到稀疏数组结构
        String[] first = null;
        try (BufferedReader br = new BufferedReader(new FileReader(new File(pathname)))) {
            String lines;
            if ((lines = br.readLine()) != null) { // 1.1 首先从读取到的稀疏数组获取第一行的第三列的有效数字(棋子数目)
                first = lines.split("\t");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 1.2 根据有效数字创建稀疏数组
        int[][] sparseArray2 = new int[Integer.parseInt(Objects.requireNonNull(first)[2]) + 1][3];
        // 2.2 根据读取到的文件填充稀疏数组数据
        try (BufferedReader br = new BufferedReader(new FileReader(new File(pathname)))) {
            int rowIndex = 0;
            String lines;
            while ((lines = br.readLine()) != null) {
                String[] split = lines.split("\t");
                sparseArray2[rowIndex][0] = Integer.parseInt(split[0]);
                sparseArray2[rowIndex][1] = Integer.parseInt(split[1]);
                sparseArray2[rowIndex][2] = Integer.parseInt(split[2]);
                rowIndex++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 2.3 打印从文件中读取到的稀疏数组
        System.out.println("打印读取到的稀疏数组: ");
        for (int[] ints : sparseArray2) {
            System.out.printf("%d\t%d\t%d\n", ints[0], ints[1], ints[2]);
        }
        // 2.4 讲稀疏数组转换成原始数组(棋盘)
        int[][] chessArray2 = new int[sparseArray2[0][0]][sparseArray2[0][1]];
        for (int i = 1; i < sparseArray2.length; i++) {
            // 稀疏数组的第i行第1列的值是原始数组的行, 稀疏数组的第i行第2列的值是原始数组的列, 稀疏数组的第i行第3列的值是原始数组的值
            chessArray2[sparseArray2[i][0]][sparseArray2[i][1]] = sparseArray2[i][2];
        }
        // 5. 打印原始数组(棋盘)
        System.out.println("打印原始数组: ");
        for (int i = 0; i < sparseArray2[0][0]; i++) {
            for (int j = 0; j < sparseArray2[0][1]; j++) {
                System.out.printf("%d\t", chessArray2[i][j]);
            }
            System.out.println();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值