Sparse and two-dimensional arrays

Keywords: Java Algorithm Rust

Two-dimensional arrays can store a lot of things, such as Go, Gobang, the whole board can be seen as a huge two-dimensional array, in which black and white chess can be represented by the number 1 or 2 in the array, blank space is generally represented by 0. But because the board is large, the corresponding two-dimensional array is also large, if stored in this way, the two-dimensional array is stored.There is a lot of data, but it also stores a lot of duplicates, so it is a waste and inefficient for programs. The same is true for maps.

As we have said, programs are designed to solve problems in life more easily and efficiently.

Take a chestnut, for example, and now there's a two-dimensional array like this

This is an array of 10*10 named Arr1, where only Arr1[0][4]=1,Arr1[1][2]=1, and the rest of the space has values of 0.

That is, this array takes up a lot of space, but most of the space is useless.

So in order to compress arrays, improve performance, sparse arrays appear, and the problem is solved.

A sparse array is a fixed-looking array, row,col,value, whose column coordinates are fixed. The first row stores the rows, columns, and the number of valid columns of the original two-dimensional array. The second row begins to store the position and value of each coordinate. So the sparse array is spareArr int[sum+1][3], where sum is the valid number plus 1.

2-D Array Sparse Array

1. Traverse through a two-dimensional array to get the valid number sum

2. Then create the sparse array spareArr int[sum+1][3]

3. Store valid two-dimensional arrays in sparse arrays

Sparse Array Variable Two-Dimensional Array

1.Read the first row of the sparse array first, and create the original two-dimensional array based on the data of the first row

2.Reads the data in the last few rows of a sparse array and assigns it to the original two-dimensional array

The code is as follows:

public class SparseArr {
    public static void main(String[] args) {
        //Original Array
        int Arr1[][]=new int[10][10];
        Arr1[0][4]=1;
        Arr1[1][2]=1;
        System.out.println("Original two-dimensional array~~");
        //Output original array
//        for (variable type variable name: array name) {
//            Loop statements that need to be executed;
//        }
//        for(int[] row:Arr1){
//            for(int:Arr1){
//        }
//        for (int[] row : Arr1) {
//            for (int data : row) {
//                System.out.printf("%d\t", data);
//            }
//            System.out.println();
//        }
        //%d output integer,%f output floating point,%n means line change
        for(int i=0;i<10;i++){
            for(int j=0;j<10;j++)
            {
                System.out.printf("%d\t",Arr1[i][j]);
            }
            System.out.println();
        }

        // 1. Convert a two-dimensional array to a sparse array
        int sum=0;//Count valid numbers for a two-dimensional array
        for(int i=0;i<10;i++){
            for(int j=0;j<10;j++)
            {
                if(Arr1[i][j] !=0){
                    sum++;
                }
            }
        }
        //2. Create a sparse array
        int sparseArr [][]=new int[sum+1][3];
        sparseArr [0][0]=10;
        sparseArr [0][1]=10;
        sparseArr [0][2]=sum;
        // Traverse a two-dimensional array and store non-zero values in sparseArr
        int count=0;
        for(int i=0;i<10;i++){
            for(int j=0;j<10;j++)
            {
                if(Arr1[i][j] !=0){
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = Arr1[i][j];
                }
            }
        }

        // Output Sparse Array
//        int a[2][2];
//        int len1=a.length; //Represents the total number of rows (column length)
//        int len2=a[i].length. //Represents the total number of columns (row length)
        for(int i=0;i<sparseArr.length; i++){
            System.out.printf("%d\t%d\t%d\t\n", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);
        }

        //Convert a sparse array to a two-dimensional array
        int Arr2[][]=new int[sparseArr [0][0]][ sparseArr [0][1]];
        //Traverse from the second row of a sparse array
        for (int i=1;i<sparseArr.length;i++){
            Arr2[sparseArr[i][0]][sparseArr[i][1]]=sparseArr [i][2];
        }
        //Print Arr2[][]
//        for (int i = 0; i < sparseArr [0][0]; i++) {
//            for (int j = 0; j < sparseArr [0][0]; j++) {
//                System.out.printf("%d",Arr2[i][j]);
//            }
//            System.out.println();
//        }
        for (int[] number:Arr2
             ) {
            for(int data:number){
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }
    }
}

 

 

Posted by RobM on Wed, 15 Sep 2021 13:21:12 -0700