Conversion between sparse array and two-dimensional array

Convert sparse array to two-dimensional array

Two-dimensional arrayTwo-dimensional array

Convert the corresponding sparse array

Corresponding to the two-dimensional array above

Conversion rules

1. The numbers corresponding to the first row of the sparse array are the number of rows (11), the number of columns (11), and the total number of values ​​(5) of the two-dimensional array.

2. Starting from the second row of the sparse array, each row represents a value in a two-dimensional array. For example, 1 1 1 in the second row
represents a two-dimensional array whose subscript is chessarray[1[1]. The value is 1, that is chessarray[1[1]=1; and so on, each row is the location of a value in the two-dimensional array.
## Code

int chessArray[][] = new int[11][11];
    //赋值
    {
        chessArray[1][1]=1;
        chessArray[2][3]=2;
        chessArray[3][3]=3;
        chessArray[4][4]=4;
        chessArray[6][6]=5;
    }
    //遍历计数总值
     int dataNum = 0;
        System.out.println("------------chessArray--------------");
        for(int rows=0;rows < chessArray.length ; rows ++){
            for (int col = 0 ;col < chessArray[rows].length; col++){
                System.out.print(chessArray[rows][col]+" ");
                if(chessArray[rows][col] != 0)
                    dataNum++;
            }
            System.out.println();
        }
        //开始转换成稀疏数组
        int parseArray[][] = new int[dataNum+1][3];//定义稀疏数组
        int sparseRow = 0;//稀疏数组第一行赋值
        parseArray[sparseRow][0] =chessArray.length;
        parseArray[sparseRow][1] = chessArray[0].length;
        parseArray[sparseRow][2] = dataNum;
        //二维数组中的值存储到稀疏数组
        for(int rows=0;rows < chessArray.length ; rows ++){
            for (int col = 0 ;col < chessArray[rows].length; col++){
                if(chessArray[rows][col] != 0){
                    sparseRow++;
                    parseArray[sparseRow][0]=rows;
                    parseArray[sparseRow][1]=col;
                    parseArray[sparseRow][2]=chessArray[rows][col];
                }
            }
        }
        //打印稀疏数组
        System.out.println("------------parseArray--------------");
        for (int[] row : parseArray){
            for(int data: row){
                System.out.print(data+" ");
            }
            System.out.println();
        }

The two-dimensional array is inserted here to convert the code piece into a sparse array

Follow the above code to achieve the inversion; convert the sparse array into a two-dimensional array

//定义一个稀疏数组
 int[][] sparseArray = new int[6][3];
        //初始化二维数组
        sparseArray[0][0]= 11 ;
        sparseArray[0][1]= 11 ;
        sparseArray[0][2]= 5 ;
        //赋值开始 二位数组的一个值 对应稀疏数组的一行
        sparseArray[1][0]= 1 ;
        sparseArray[1][1]= 1 ;
        sparseArray[1][2]= 1 ;
        //赋值开始 二位数组的一个值
        sparseArray[2][0]= 2 ;
        sparseArray[2][1]= 3 ;
        sparseArray[2][2]= 2 ;
        //赋值开始 二位数组的一个值
        sparseArray[3][0]= 3 ;
        sparseArray[3][1]= 3 ;
        sparseArray[3][2]= 3 ;
        //赋值开始 二位数组的一个值
        sparseArray[4][0]= 4 ;
        sparseArray[4][1]= 4 ;
        sparseArray[4][2]= 4 ;
        //赋值开始 二位数组的一个值
        sparseArray[5][0]= 6 ;
        sparseArray[5][1]= 6 ;
        sparseArray[5][2]= 6 ;

        //初始化二维数组
        int[][] chessArray = new int[sparseArray[0][0]][sparseArray[0][1]];
        System.out.println("------------sparseArray--------------");
        for(int i = 0;i < sparseArray.length; i++){
            if(i!=0){//稀疏数组与二维数组的转换
                int row = sparseArray[i][0] ;
                int col = sparseArray[i][1];
                int data = sparseArray[i][2];
                chessArray[row][col] = data;
            }
            for (int j =0 ;j < sparseArray[i].length ; j++){
                System.out.printf("%d\t",sparseArray[i][j]);
            }
            System.out.println();

        }
        //把二维数组打印出来
        System.out.println("------------chessArray--------------");
        for(int rows=0;rows < chessArray.length ; rows ++){
            for (int col = 0 ;col < chessArray[rows].length; col++){
                System.out.printf("%d\t",chessArray[rows][col]);
            }
            System.out.println();
        }


Guess you like

Origin blog.csdn.net/weixin_40128696/article/details/107707894
Recommended