| - Applicable scene
When there are a large number of duplicate array of digital sparse array can be used to compress the array, thus reducing the consumption of resources
| - Notes
| - Code
. 1 Package Structure; 2 . 3 / ** . 4 * 9527 :: @auther . 5 * @Description: sparse array . 6 * @program: news_chapter06_02 . 7 * @Create: 2019-10-01 17:26 . 8 * / . 9 public class SparseArry { 10 public static void main (String [] args) { 11 // create an original two-dimensional array of 11 * 11 12 is @ 0: 1 representing no pieces 2 represents represents sunspots basket 13 is int chessArr1 [] [] = new new int [. 11] [. 11 ]; 14 chessArr1 [. 1] [2] =. 1 ; 15 chessArr1 [2] [. 3] = 2 ; 16 chessArr1 [. 4] [. 5] = 2 ; . 17 18 is // output the original two-dimensional array . 19 System.out.println ( "original two-dimensional array:" ); 20 for ( int [] Row: chessArr1) { 21 is for ( int Data: Row) { 22 is System.out.printf ( "% D \ T" , Data); 23 is } 24 System.out.println (); 25 } 26 is 27 // two-dimensional array to a sparse array 28 // 1, to obtain two-dimensional array to traverse the number of non-zero data 29 int SUM = 0; // counter 30 for ( int I = 0; I <. 11; I ++ ) { 31 is for ( int J = 0; J <. 11; J ++ ) { 32 IF (chessArr1 [I] [J] =! 0 ) { 33 is SUM ++ ; 34 is } 35 } 36 } 37 [ System.out.println ( "total number of non-0" + sum + "a" ); 38 is 39 // 2, creates a corresponding sparse array 40 int sparseArr [] [] = new new int [SUM +. 1] [. 3]; //An effective amount of data rows = number of columns + 1 = 3 41 is // a sparse array assignment first line: sequentially storing: rows and columns, the number of valid data 42 is sparseArr [0] [0] =. 11 ; 43 is sparseArr [0] [ . 1] =. 11 ; 44 is sparseArr [0] [2] = SUM; 45 46 is // traverse the two-dimensional array, a non-zero value stored in the sparseArr 47 int COUNT = 0; // recording of a few non-zero number data 48 for ( int I = 0; I <. 11; I ++ ) { 49 for ( int J = 0; J <. 11; J ++ ) { 50 IF (! chessArr1 [I] [J] = 0 ) { 51 is COUNT ++ ; 52 is sparseArr [COUNT] [0] = I; // row coordinate 53 is sparseArr [COUNT] [. 1] = J; // column coordinate 54 is sparseArr [COUNT] [2] = chessArr1 [I] [J]; // coordinate value corresponding to 55 } 56 } 57 } 58 59 // output form the sparse array 60 System.out.println ( "******** dividing line ------ ------- I - ******** " ); 61 is System.out.println (" sparse array is obtained: " ); 62 is for ( int I = 0; I <sparseArr.length; I ++ ) { 63 is System.out.printf ( "% D \ D% T \ T% D \ T \ n-", sparseArr [I] [0], sparseArr [I] [. 1], sparseArr [I] [2 ]); 64 } 65 66 67 // will return to the original sparse array of two-dimensional array 68 / ** 69 * ideas: 1, first read the first line of the sparse array, according to the data of the first line, creating an original two-dimensional array of 70 * 2, continues to read after a few sparse array rows, and assigned to the original two-dimensional array 71 * / 72 // read the first row 73 is int afterArr [] [] = new new int [sparseArr [0] [0] ] [sparseArr [0] [. 1 ]]; 74 75 // read a sparse array and assigned to the remaining two-dimensional array, the first row is stored coordinate information, the assignment starting from the second line 76 for ( int i = 1; i <sparseArr.length; i ++) { 77 afterArr [sparseArr [I] [0]] [sparseArr [I] [. 1]] = sparseArr [I] [2 ]; 78 } 79 80 System.out.println ( "******** ****************** \ the n-" ); 81 // output reduction of about two-dimensional array and consistent look whether the original array 82 for ( int [] Row: afterArr) { 83 for ( int Data: Row) { 84 System.out.printf ( "% D \ T" , Data); 85 } 86 System.out.println (); 87 } 88 } 89 }