稀疏数组

用于压缩含有大量空元素的二维数组

稀疏数组的列数是确定的为3。
第0行用于记录原数组的行数、列数、有效数据个数。

sparseArr[0][0]   //记录原二维数组的 行数
sparseArr[0][1]   //记录原二维数组的 列数
sparseArr[0][2]   //记录原二维数组的 有效数据个数

后面每一行为一条记录,用于记录二维数组中的一个有效元素。
第0列记录行下标,第1列记录列下标,第2列记录数值。

sparseArr[i][0]   //记录在原数组中的 行下标
sparseArr[i][1]   //记录在原数组中的 行下标
sparseArr[i][2]   //记录在原数组中的 数值

代码实现:

Demo1
public class SparseArray {

    public static void main(String[] args) {
        //创建一个二维数组
        //   0表示没有棋子   1黑子   2蓝子
        int chessArr1[][] = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        chessArr1[4][7] = 1;
        chessArr1[9][6] = 2;
        //输出二维数组
        for (int[] row : chessArr1) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }



        //将二维数组转化为稀疏数组
        //1.先求出有效元素的个数
        int sum = 0;
        for (int i = 0; i < chessArr1.length; i++) {
            for (int j = 0; j < chessArr1[i].length; j++) {
                if(chessArr1[i][j] != 0) sum++;
            }
        }
        //2.创建对应的稀疏数组
        int sparseArr[][] = new int[sum + 1][3];
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;
        //3.遍历二维数组,将非0的值放到sparseArr
        int count = 0;  //用于记录是第几个非零元素
        for (int i = 0; i < chessArr1.length; i++) {
            for (int j = 0; j < chessArr1[i].length; j++) {
                if(chessArr1[i][j] != 0) {
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArr1[i][j];
                }
            }
        }
        //4.输出稀疏数组
        System.out.println("\n\n得到的稀疏数组为:");
        for (int i = 0; i < sparseArr.length; i++) {
            System.out.printf("%d\t%d\t%d\n", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);
        }



        //将稀疏数组还原
        //1.依据第一行的信息建立二维数组
        int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];
        //2.读取稀疏数组中记录的数据,并赋给二维数组
        for (int i = 1; i < sparseArr.length; i++) {
            for (int j = 0; j < 3; j++) {
                chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
            }
        }
        //3.输出恢复后的二维数组
        System.out.println("\n\n恢复后的数组为:");
        for(int[] row : chessArr2) {
            for (int data : row)
                System.out.printf("%d\t", data);
            System.out.print("\n");
        }



    }//main()

}

Demo2 写入文件,持久储存
class SparseArrayMyPractice{

    private static final int N = 11;

    public static void main(String[] args) {
        SparseArrayMyPractice myPractice = new SparseArrayMyPractice();
        int chessArr1[][] = myPractice.create();
        myPractice.save(chessArr1);
        myPractice.read();
    }

    public int[][] create() {
        //创建一个二维数组
        //   0表示没有棋子   1黑子   2蓝子
        int chessArr1[][] = new int[N][N];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        chessArr1[4][7] = 1;
        chessArr1[9][6] = 2;
        //打印
        System.out.println("原数组为:");
        for (int[] row : chessArr1) {
            for (int data : row)
                System.out.printf("%d\t", data);
            System.out.println();
        }


        return  chessArr1;
    }

    public void save(int chessArr1[][]) {
        //先转化为一个稀疏数组
        int sum = 0;
        for (int[] row : chessArr1)
            for (int data : row)
                if(data != 0) sum++;
        int sparseArr[][] = new int[sum+1][3];
        sparseArr[0][0] = N;
        sparseArr[0][1] = N;
        sparseArr[0][2] = sum;
        int count = 0;
        for (int i = 0; i < chessArr1.length; i++) {
            for (int j = 0; j < chessArr1[i].length; j++) {
                if (chessArr1[i][j] != 0) {
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArr1[i][j];
                }
            }
        }
        //打印
        System.out.println("\n\n转化为稀疏数组:");
        for (int[] row : sparseArr) {
            for (int data : row)
                System.out.printf("%d\t", data);
            System.out.println();
        }
        //保存到文件
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream("map.data");
            oos = new ObjectOutputStream(fos);
            oos.writeObject(sparseArr);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) oos.close();
                if (fos != null) fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void read() {
        //从二进制文件读入
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        int sparseArr[][] = null;
        try {
            fis = new FileInputStream("map.data");
            ois = new ObjectInputStream(fis);
            sparseArr = (int[][]) ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ois != null) ois.close();
                if (fis != null) fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //输入稀疏数组
        System.out.println("\n\n从二进制文件读入的稀疏数组为:");
        for (int[] row : sparseArr) {
                System.out.printf("%d\t%d\t%d\n", row[0], row[1], row[2]);
        }
        //开始还原
        int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];
        for (int i = 1; i < sparseArr[0][2]; i++) {
            chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }
        //打印还原后的二维数组
        System.out.println("\n\n还原后的二维数组为:");
        for (int[] row : chessArr2) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }

    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值