稀疏数组说明及代码实现

稀疏sparsearray数组

1.先看一个实际的需求

编写的五子棋程序中,有存盘和续上盘的功能
在这里插入图片描述
分析问题:
因为该二维数组的很多值是默认0,因此记录了很多没有意义的数据->稀疏数组

2.基本介绍
当一个数组中大部分元素为0,或则为同一值的数组时,可以使用稀疏数组来保存该数组。
稀疏数组的处理方法时:
(1)记录数组一共有几行几列,有多少个不同的值。
(2)把具有不同值的元素的行列及值记录在一个小规模的数组中(这个数组就是稀疏数组),从而缩小程序的规模。

稀疏数组的举例说明:
在这里插入图片描述
3.应用实列
(1)使用稀疏数组,来保留类似前面的二维数组(棋盘、地图等等)
(2)把稀疏数组存盘,并且可以从新恢复原来的二维数据
(3)整体思路分析

在这里插入图片描述
(4)代码实现

public class SparseArray {
    public static void main(String[] args) throws Exception {
        //创建一个原始的二维数组11*11
        //0:表示没有棋子,1表示 黑子 2 表示蓝子
        int[][] cherryArr = new int[11][11];
        cherryArr[1][2] = 1;
        cherryArr[2][3] = 2;
        //输出原始数组
        System.out.println("原始的二维数组:");
        for (int[] arr : cherryArr) {
            System.out.println(Arrays.toString(arr));
        }
        //将二维数组转稀疏数组
        //先遍历二维数组得到非0数据的个数
        int sum = 0;
        for (int i = 0; i < cherryArr.length; i++) {
            for (int j = 0; j < cherryArr[i].length; j++) {
                if (cherryArr[i][j] != 0){
                    sum++;
                }
            }
        }
        //创建稀疏数组
        int[][] sparseArr = new int[sum+1][3];
        //给稀疏数组赋值
        sparseArr[0][0] = cherryArr.length;
        sparseArr[0][1] = cherryArr[0].length;
        sparseArr[0][2] = sum;
        //遍历二维数组,将非0的值存放到sparseArr中
        int oneIndex = 1;
        for (int i = 0; i < cherryArr.length; i++) {
            for (int j = 0; j < cherryArr[i].length; j++) {
                if (cherryArr[i][j] != 0){
                    sparseArr[oneIndex][0] = i;
                    sparseArr[oneIndex][1] = j;
                    sparseArr[oneIndex][2] = cherryArr[i][j];
                    oneIndex++;
                }
            }
        }
        //输出稀疏数组
        System.out.println("稀疏数组:");
        for (int[] arr : sparseArr) {
            System.out.println(Arrays.toString(arr));
        }
        //把稀疏数组存储到文件上
        File file = new File("array.db");
        if (!file.exists()){
            writeFile(sparseArr);
        }
        //读取稀疏数组
        int[][] readFile = readFile();
        if (readFile == null){
            throw new Exception("文件可能损坏");
        }
        //将稀疏数组恢复为原始数组
        int[][] recoveryCherryArr = new int[readFile[0][1]][readFile[0][1]];
        for (int i = 1; i <readFile.length; i++) {
            recoveryCherryArr[readFile[i][0]][readFile[i][1]] = readFile[i][2];
        }
        //恢复后的稀疏数组
        System.out.println("恢复后的稀疏数组:");
        for (int[] arr : recoveryCherryArr) {
            System.out.println(Arrays.toString(arr));
        }
    }
    public static void writeFile(int[][] sparseArr){
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream("array.db");
            oos = new ObjectOutputStream(fos);
            oos.writeObject(sparseArr);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    oos = null;
                }
            }
            if (fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    fos = null;
                }
            }
        }
    }
    public static int[][] readFile(){
        FileInputStream fis = null;
        ObjectInputStream ois =null;
        int[][] sparseArr = null;
        try {
            fis = new FileInputStream("array.db");
            ois = new ObjectInputStream(fis);
            sparseArr = (int[][]) ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    ois = null;
                }
            }
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    fis = null;
                }
            }
        }
        return sparseArr;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值