Sparse array (java implementation)

Posted by litebearer on Thu, 10 Oct 2019 16:30:17 +0200

1. Sparse Array

Sparse arrays can be used to save an array when most of the elements in an array are 0 or the same value. Sparse arrays are processed by:

1.1 Record array has several rows and columns, and how many different values

1.2 Record rows, columns and values of elements with different values in a small array, thus reducing the size of the program I

2. Thoughts on Transforming Two-Dimensional Array to Sparse Array

2.1. Traverse the original two-dimensional array and sum the number of valid data

2.2. Sparse Arr int can be created based on sum sum+1

2.3. Store valid data from two-dimensional arrays into sparse arrays

3. Thoughts on Converting Sparse Array to Primitive Two-Dimensional Array

3.1. Read the first row of the sparse array and create the original two-dimensional array based on the data of the first row, such as chessAr2=int above. 11

3.2. Read the sparse array and assign it to the original two-dimensional array.

4. Application examples

Sparse arrays are used to save the sparse arrays (chessboard, map, etc.) similar to the previous two-dimensional arrays, and to restore the original two-dimensional arrays to the whole idea analysis.

public class SparseArray {
    public static void main(String[] args) {
//Create a two-dimensional array 1 for black 2 for blue 0 for blank
        int chessarr1[][] = new int[11][11];
        chessarr1[1][2] = 1;
        chessarr1[2][3] = 2;
        //Traversal Cyclic Two-Dimensional Array
        System.out.println("The original two-dimensional array is");
        for (int[] row : chessarr1) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }
        //Traverse the original two-dimensional array to get the number of non-zero
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessarr1[i][j] != 0) {
                    sum++;
                }
            }
        }
        //Create the corresponding sparse array
        int sparsearr[][] = new int[sum + 1][3];
        //Copy sparse arrays
        sparsearr[0][0] = 11;
        sparsearr[0][1] = 11;
        sparsearr[0][2] = sum;
        //Traversing two-dimensional arrays places non-zero values in sparsearr in
        int count=0;//For recording the number of non-zero data
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if(chessarr1[i][j]!=0){
                    count++;
                    sparsearr[count][0]=i;
                    sparsearr[count][1]=j;
                    sparsearr[count][2]=chessarr1[i][j];
                }
            }
            }
        //Output sparse array form
        System.out.println();
        System.out.println("The sparse array is");
        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]);
        }
        System.out.println();


        //Converting sparse arrays to two-dimensional arrays
        int chessarr2[][]=new int[sparsearr[0][0]][sparsearr[0][1]];
        //Traversing sparse arrays from the second line
        for(int i=1;i<sparsearr.length;i++){
            chessarr2[sparsearr[i][0]][sparsearr[i][1]]=sparsearr[i][2];
        }
         //The sparse array after recovery is
      for (int[] row : chessarr2) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }
        }
    }

Topics: Java