稀疏数组

1、简介

当一个数组中的大部分元素为0,或者为同一个值时,可以使用稀疏数组来保存该数组

稀疏数组的处理方法是:

1)记录该数组一共几行几列,有多少个不同的值

2)把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩减程序的规模

clipboard

原理图:

clipboard

2、稀疏数组的代码实现

public class SparseArray {
    public static void main(String[] args) {
        //创建一个原始的二维数组 11x11
        //0: 表示没有棋子  1:表示黑子   2:表示白子
        int chessArray1[][]= new int[11][11];
        chessArray1[1][2] = 1;
        chessArray1[2][3] =2;

        //输出原始的二维数组
        for(int i=0;i<chessArray1.length;i++){
            for(int j=0;j<11;j++){
                System.out.printf("%d	",chessArray1[i][j]);
            }
            System.out.println();
        }


        //将二维数组转成稀疏数组
        //1、遍历二维数组,得到非0的个数
        int sum= 0;
        for(int i=0;i<chessArray1.length;i++){
            for(int j=0;j<11;j++){
               if(chessArray1[i][j]!=0){
                   sum++;
               }
            }
        }
        //2、创建对用的稀疏数组 int sparseArr[sum+1][3]
        int sparseArray[][] = new int[sum+1][3];
        //3、给稀疏数组赋值
        //3.1 给第一行赋值
        sparseArray[0][0] = 11;
        sparseArray[0][1] = 11;
        sparseArray[0][2] = sum;
        //3.2 给剩余的其他行赋值
        int count = 1; //记录稀疏数组的行号
        for(int i=0;i<chessArray1.length;i++){
            for(int j=0;j<11;j++){
                if(chessArray1[i][j]!=0){
                    sparseArray[count][0]=i;
                    sparseArray[count][1]=j;
                    sparseArray[count][2]=chessArray1[i][j];
                    count++;
                }
            }
        }
        //输出稀疏数组
        System.out.println("得到的稀疏数组为:");
        for(int i=0;i<sparseArray.length;i++){
            for(int j=0;j<3;j++){
                System.out.printf("%d	",sparseArray[i][j]);
            }
            System.out.println();
        }

        //将稀疏数组恢复成原始的二位数组
        //1、先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组
        int[][] chessArray2 =new int[sparseArray[0][0]][sparseArray[0][1]];
        //2、将稀疏数组的后几行数据,赋值给原来的二维数组
        for(int i=1;i<sparseArray.length;i++){
            chessArray2[sparseArray[i][0]][sparseArray[i][1]]=sparseArray[i][2];
        }
        //输出复原后的二维数组
        System.out.println("复原后的二维数组为:");
        for(int i=0;i<chessArray2.length;i++){
            for(int j=0;j<11;j++){
                System.out.printf("%d	",chessArray2[i][j]);
            }
            System.out.println();
        }
    }
}

clipboard

3、课后练习

clipboard

public class ArrayStore {
    public static void main(String[] args) throws IOException, IOException {
        //创建一个原始的二维数组 11x11
        //0: 表示没有棋子  1:表示黑子   2:表示白子
        int chessArray1[][]= new int[11][11];
        chessArray1[1][2] = 1;
        chessArray1[2][3] =2;

        //输出原始的二维数组
        for(int i=0;i<chessArray1.length;i++){
            for(int j=0;j<11;j++){
                System.out.printf("%d	",chessArray1[i][j]);
            }
            System.out.println();
        }
        //将二维数组转成稀疏数组
        //1、遍历二维数组,得到非0的个数
        int sum= 0;
        for(int i=0;i<chessArray1.length;i++){
            for(int j=0;j<11;j++){
                if(chessArray1[i][j]!=0){
                    sum++;
                }
            }
        }
        //2、创建对用的稀疏数组 int sparseArr[sum+1][3]
        int sparseArray[][] = new int[sum+1][3];
        //3、给稀疏数组赋值
        //3.1 给第一行赋值
        sparseArray[0][0] = 11;
        sparseArray[0][1] = 11;
        sparseArray[0][2] = sum;
        //3.2 给剩余的其他行赋值
        int count = 1; //记录稀疏数组的行号
        for(int i=0;i<chessArray1.length;i++){
            for(int j=0;j<11;j++){
                if(chessArray1[i][j]!=0){
                    sparseArray[count][0]=i;
                    sparseArray[count][1]=j;
                    sparseArray[count][2]=chessArray1[i][j];
                    count++;
                }
            }
        }
        //输出稀疏数组
        System.out.println("得到的稀疏数组为:");
        for(int i=0;i<sparseArray.length;i++){
            for(int j=0;j<3;j++){
                System.out.printf("%d	",sparseArray[i][j]);
            }
            System.out.println();
        }

        //文件的路径
        String str = "d:/sparse.txt";
        File file = new File(str);
        //如果磁盘中该文件不存在,则创建该文件
        if (!file.exists()){
            file.createNewFile();
        }
       //创建字符写入流对象
        FileWriter fileWriter = new FileWriter(str);
        for (int i = 0; i < sparseArray.length; i++) {
            for (int j = 0; j < sparseArray[i].length; j++) {
                //写入每个字符
                fileWriter.write(sparseArray[i][j]);
            }
        }
        //关闭写入流
        fileWriter.close();
        System.out.println("写入完毕");

        //创建新的数组,来接收读取的数据
        int [][] sparseArr2 = new int[sum+1][3];
        FileReader fileReader = new FileReader(str);
        for (int i = 0; i < sparseArr2.length; i++) {
            for (int j = 0; j < sparseArray[i].length; j++) {
                //把读取的数据设置到数组中
                sparseArr2[i][j] = fileReader.read();
            }
        }

        System.out.println("从文件读出的稀疏数组为:");
        for(int i=0;i<sparseArr2.length;i++){
            for(int j=0;j<3;j++){
                System.out.printf("%d	",sparseArr2[i][j]);
            }
            System.out.println();
        }
        //关闭读取流
        fileReader.close();
    }
}
原文地址:https://www.cnblogs.com/houchen/p/13379390.html