Data structure conversion between two-dimensional array and sparse array

Why convert a two-dimensional array to a sparse array?

When using arrays in reality, most of the arrays do not fill all the space, which will lead to the possibility of traversing all positions when querying valid data. Obviously there is no necessary consumption, so this Sometimes it needs to be sparse to perform the conversion.

It can be seen from the figure that the original two-dimensional array on the left is 6*7=42, and the sparse array on the right is 9*3=27. Obviously it takes much less time to traverse 27 than to traverse 42. At this time The role of sparse arrays is shown

What is a sparse array?

When most of the elements in an array are 0 or an array of the same value, you can use a sparse array to save the array.

The sparse array processing method is: the
first row of record array has a total of several rows and several columns, how many
different values ​​are there, and record the rows, columns and values ​​of elements with different values ​​in a small array, thereby reducing the size of the program

How to connect a two-dimensional array and a sparse array to each other?

Two-dimensional array -> sparse array

1. Traverse the original two-dimensional array to get the number of valid data Sum
2. Create a sparse array based on Sum sparseArr = int[sum+1][3]
3. Store the valid data of the original two-dimensional array into the sparse array

Sparse array -> two-dimensional array

1. Read the first row of the sparse array and create a two-dimensional array based on the data of the first row.
2. Assign the data of the second and subsequent rows of the sparse array to the two-dimensional array

Well, not much gossip, just go to the code!

package com.xiaozhao.datastructure;

import jdk.management.resource.internal.inst.SocketOutputStreamRMHooks;

/**
 * @author : Carson-Zhao
 * @date : 2020/7/15 21:49
 */
public class SparseArray {
    public static void main(String[] args) {
        /**
         * 创建一个原始的二维数组 11*11
         * 0:表示没有棋子
         * 1:表示黑子
         * 2:表示白子
         *
         */
        int[][] chessArray = new int[11][11];
        chessArray[1][2] = 1;
        chessArray[2][3] = 2;
        chessArray[5][8] = 9;
        for(int[] c : chessArray){
            for (int data : c){
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }

        /**
         * 将二维数组转换为稀疏数组
         * 1.遍历原始二位数数组,得到非零数据的个数
         * 2.创建稀疏数组
         * 3.将原始数组的有效数据赋值给稀疏数组
         */
        int sum = 0;
        for(int i = 0;i < chessArray.length;i++){
            for (int j = 0; j < chessArray.length; j++) {
                if(chessArray[i][j] != 0){
                    sum++;
                }
            }
        }
        int[][] sparseArray = new int[sum+1][3];
        //给稀疏数组赋值
        sparseArray[0][0] = 11;
        sparseArray[0][1] = 11;
        sparseArray[0][2] = sum;
        //遍历二维数组,拿到有效数据
        int count = 0;//用于记录第几个非零数据
        for(int i = 0;i < chessArray.length;i++){
            for (int j = 0; j < chessArray.length; j++) {
                if(chessArray[i][j] != 0){
                    count++;
                    sparseArray[count][0] = i;
                    sparseArray[count][1] = j;
                    sparseArray[count][2] = chessArray[i][j];
                }
            }
        }
        System.out.println();
        //输出转换后的稀疏数组
        System.out.println("输出转换后的稀疏数组");
        System.out.println();
        for (int i = 0; i < sparseArray.length; i++) {
            System.out.printf(
                    "%d\t%d\t%d\t",
                    sparseArray[i][0],
                    sparseArray[i][1],
                    sparseArray[i][2]
            );
            System.out.println();
        }

        System.out.println();
        System.out.println("将稀疏数组恢复成二维数组");
        System.out.println();
        //将稀疏数组恢复成二维数组
        int N = sparseArray[0][0];
        int M = sparseArray[0][1];


        int[][] array = new int[N][M];

        for (int i = 1; i < sparseArray.length; i++) {

                array[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];

        }
        for(int[] c : array){
            for (int data : c){
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }


    }
}

The implementation effect is as follows:

D:\Environment\Java\jdk1.8.0_251\bin\java.exe "

0	0	0	0	0	0	0	0	0	0	0	
0	0	1	0	0	0	0	0	0	0	0	
0	0	0	2	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	9	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	

输出转换后的稀疏数组

11	11	3	
1	2	1	
2	3	2	
5	8	9	

将稀疏数组恢复成二维数组

0	0	0	0	0	0	0	0	0	0	0	
0	0	1	0	0	0	0	0	0	0	0	
0	0	0	2	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	9	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	

Process finished with exit code 0

The specific implementation has been annotated in the code, so I won't repeat it at the end. . . . .

I learn because of food, I am Xiao Zhao, I hope to encourage each other!

Guess you like

Origin blog.csdn.net/weixin_43562937/article/details/107372529