Array implement sparse arrays

  . 1  public  class SparseArray {
   2  
  . 3      // dimensional array into a sparse array 
  . 4      public  static  void TwoArrayToSparseArray () {
   . 5          // define the original two-dimensional array is converted 
  . 6          int [] [] = originalArray new new  int [. 11] [. 11 ];
   . 7          // analog data to a two-dimensional array stored 
  . 8          originalArray [. 3] [. 4] =. 1 ;
   . 9          originalArray [. 6] [. 7] = 2 ;
 10          // number of two-dimensional array to traverse different values obtained for creating a sparse array 
. 11          int COUNT = 0; // record the number of distinct values of 
12 is          for (int [] INTS: originalArray) {
 13 is              for ( int Item: INTS) {
 14                  IF ! (Item = 0 ) {
 15                      COUNT ++ ;
 16                  }
 . 17              }
 18 is          }
 . 19  
20 is          // create a sparse array, the sparse array format is N + 1 and a fixed line 3
 21          @ different numbers in the second column of the original array stored in third column of the original array stored value in the first column of the original array stored row
 22          // sparse arrays for storing the first line of the original array the number of rows and columns and different values of
 23          // remaining N rows depends on the number of different values and different row for each row of stored values for different values of 
24          int [] [] = SparseArray new new  int [COUNT +. 1] [3];
 25  
26 is          // number of ranks and assigned different values of the original array to the first row of the sparse array 
27          SparseArray [0] [0] = originalArray.length;
 28          SparseArray [0] [. 1] = originalArray [0 ]. length;
 29          SparseArray [0] [2] = COUNT;
 30  
31 is          // sparse array holding the data line by line is incremented to index the first row has been added because it starts from the second row 
32          int index = 1 ;
 33 is  
34 is          // iterate the original array data store 
35          for ( int I = 0; I <originalArray.length; I ++ ) {
 36              for ( int J = 0; J <originalArray.length; J ++ ) {
37 [                  IF (originalArray [I] [J]! = 0 ) {
 38 is                      // find the assignment of different values to the first column of the sparse array is an array of the second column of the original row is the original column of the array
 39                      // third column is different from the original array value is then indexed increment 
40                      SparseArray [index] [0] = I;
 41 is                      SparseArray [index] [. 1] = J;
 42 is                      SparseArray [index] [2] = originalArray [I] [J];
 43 is                      index ++ ;
 44 is                  }
 45              }
 46 is          }
 47  
48          System.out.println ( "Print completed sparse array conversion from two-dimensional array" );
 49          //Print sparse array 
50          for ( int I = 0; I <sparseArray.length; I ++ ) {
 51 is              System.out.println (SparseArray [I] [0] + "\ T" + SparseArray [I] [. 1] + "\ T "+ SparseArray [I] [2] +" \ T " );
 52 is          }
 53 is      }
 54 is  
55      // sparse array converts a two-dimensional array 
56 is      public  static  void SparseArrayToTwoArray () {
 57 is          // create a sparse array 
58        int [] [ ] = SparseArray new new  int [. 3] [. 3 ];
 59        // analog data 
60         SparseArray [0] [0] =. 11 ;
61 is          SparseArray [0] [. 1] =. 11 ;
 62 is          SparseArray [0] [2] = 2 ;
 63 is          SparseArray [. 1] [0] =. 3 ;
 64          SparseArray [. 1] [. 1] =. 4 ;
 65          SparseArray [. 1] [ 2] =. 1 ;
 66          SparseArray [2] [0] =. 6 ;
 67          SparseArray [2] [. 1] =. 7 ;
 68          SparseArray [2] [2] = 2 ;
 69  
70          // create a two-dimensional array
 71          // first the resulting two-dimensional array of rows and columns 
72          int row = SparseArray [0] [0 ];
 73 is          int column SparseArray = [0] [. 1];
 74          int [] [] = originalArray new new  int [Row] [column];
 75  
76    // iterate sparse array to a two-dimensional array assignment because the sparse array stored in the first column is the number of different ranks of the two-dimensional array traversal value starts at 1 
77          for ( int I = 1; I <= sparseArray.length-1; I ++ ) {
 78              // each row first column of the second row third column as the column values according to row, column, assigning values to the two-dimensional array 
79               Row = SparseArray [I] [0 ];
 80              column SparseArray = [I] [. 1 ];
 81              originalArray [Row] [column] = SparseArray [I] [2 ];
 82          }
 83  
84          / / print two-dimensional array 
85         System.out.println ( "print completion sparse array conversion from two-dimensional array" );
 86          for ( int [] INTS: originalArray) {
 87              for ( int Item: INTS) {
 88                  of System.out.print (Item + "\ T " );
 89              }
 90              System.out.println ();
 91 is          }
 92      }
 93  
94  
95  
96      public  static  void main (String [] args) {
 97  
98          // two-dimensional array into a sparse array 
99          TwoArrayToSparseArray ();
 100  
101         // sparse array converting two-dimensional array 
102          SparseArrayToTwoArray ();
 103      }
 104  
105  
106  
107 }

 

Guess you like

Origin www.cnblogs.com/java888/p/11666708.html