Convert two-dimensional array of data structures and sparse array

1. The two-dimensional array

In fact, the number of high-dimensional data in a matrix, which is not too much explanation

2. sparse array

When a two-dimensional array are mostly value is 0, or the same, we can consider a sparse array used to reduce the memory

Treatment sparse array:

1) a two-dimensional array of three of the original array has recorded several odd row, how many different values

2) where the elements of the row and the column values ​​of different records in the sparse array, thereby downsizing

3. Examples of the following:

 

4. The following is a sparse array and the conversion between the two-dimensional array

Package com.ebiz.array; 

/ ** 
 * @author YHJ 
 * @Create 2019-07-13 16:46 
 * <P> 
 * sparse array 
 * / 
public  class SparseArray { 

    public  static  void main (String [] args) {
         / / Create original two-dimensional array of 11 * 11 represents sunspots represents albino 
        int [] [] = chessArray1 new new  int [11] [11 ]; 

        // first column of a second row of sunspots 
        chessArray1 [1] [2] = 1 ;
         // second row and third column has a albino 
        chessArray1 [2] [. 3] = 2 ; 

       / * System.out.println ( "original two-dimensional array");
        for (int [] Row: chessArray1) {
            for (int I: Row) { 
                System.out.printf ( "% D \ T", I); 
                //System.out.print(i+"\t "); 
            } 
            System.out.println (); 
        }  ;* / 

        // will turn into a two-dimensional array sparse arrays
         @ 1 to give the number of nonzero values of the original array 
        int SUM = 0 ;
         for ( int I = 0; I <. 11; I ++ ) {
             for ( int J = 0 ; J <. 11; J ++ ) {
                 IF (chessArray1 [I] [J] = 0! ) { 
                    SUM ++ 
                }
                    System.out.println (); 
            } 
        } 
        // System.out.println ( "SUM =" SUM +);
         // 2. Create a sparse array 
        int [] [] = SparseArray new new  int [SUM +. 1] [. 3 ] ;
         // 3. sparse array assignment
         // 3.1 0th line 
        SparseArray [0] [0] =. 11 ; 
        SparseArray [ 0] [. 1] =. 11 ; 
        SparseArray [ 0] [2] = SUM; 

        // 3.2 traversing the original array a non-null value paid sparse array 
        int COUNT = 0;   // definition of a counter, changing the number of rows 
        for ( int I = 0; I <. 11; I ++ ) {
            for ( I: Row) { intJ = 0; J <. 11; J ++ ) {
                 IF ! (chessArray1 [I] [J] = 0 ) { 
                    COUNT ++ ; 
                    SparseArray [COUNT] [ 0] = I; 
                    SparseArray [COUNT] [ . 1] = J; 
                    SparseArray [COUNT] [ 2] = chessArray1 [I] [J]; 

                } 
            } 
        } 
        // output sparse array 
        System.out.println ( "word Count array is:" );
         for ( int [] Row: SparseArray) {
             for ( int 
                System.out.printf ("% D \ T" , I); 
            } 
            System.out.println (); 
        } 

        System.out.println ( "----------------------- ------------------------------------- " ); 
        System.out.println ( " sparse array recovery " ); 

        // creates a corresponding array according to a sparse array 
        int [] [] = Chary new new  int [SparseArray [0] [0]] [SparseArray [0] [. 1 ]]; 

        // iterate sparse arrays, paid Chary 
        for ( int I =. 1; I <sparseArray.length; I ++ ) { 
            Chary [SparseArray [I] [ 0]] [SparseArray [I] [. 1]] = SparseArray [I] [2 ]; 
        } 

        // output Array 
        for (int[] row : chary) {
            for (int i : row) {
                System.out.printf("%d\t",i);
                //System.out.print(i+"\t");
            }
            System.out.println();
        }


    }
}

 

Guess you like

Origin www.cnblogs.com/jiushixihuandaqingtian/p/11203898.html