Actual demand
- analyse problem
Because a lot of the value of the two-dimensional array is the default value 0, and therefore recorded a lot of pointless data .-> sparse array .
1. Basic Introduction
When a majority of the data elements is zero, or a value with an array, the sparse array can be used to hold the array is a sparse array processing method
1. array of records, a total of several odd row, how many different values are
2. The record ranks and values element have different values in an array of small, thereby to reduce program size
Sparse array Description
2. Application Examples
- Using sparse arrays, similar to retain the previous two-dimensional array (checkerboard, maps, etc.)
- Save the sparse array, and can be re-restore the original number of two-dimensional array
- The whole idea analysis
3. Add a lot more easy to understand comments
1 public static void main(String[] args) {
2 // create an original two-dimensional array of 11 * 11
// 0 3: 1 means no pieces represented basket 2 represents sunspots
4 int chessArr1[][] = new int[11][11];
5 chessArr1[1][2] = 1;
6 chessArr1[2][3] = 2;
7 8 // output original two-dimensional array
9 for (int[] row : chessArr1){ For 10 (int Data: Row). 11 System.out.printf {( "% D \ T" , Data); 12 is } 13 is System.out.println (); 14 } 15 16 // array to a two-dimensional sparse array 17 // 1 dimensional array to traverse the number of effective data SUM = 0 int 18 is ;. 19 for (int I = 0; I <. 11; I ++ ) {20 is for (int J = 0; J <. 11; J ++ ) {! IF 21 is (chessArr1 [I] [J] = 0 ) {SUM 22 is ++ ; 23 is } 24 } 25 } 26 isSystem.out.println (sum); 27 // create a sparse array of rows equal to the number corresponding to the number of valid data +1 28 int sparseArr [] [] = new int [sum + 1] [3]; // column is fixed the first value of the second row third column values RMS value @ 29 assigned to the sparse array 30 sparseArr [0] [0] = 11; // behavior 11 31 sparseArr [0] [1 ] = 11; // as 11 32 sparseArr [0] [2 ] = sum; // first row and third column 3334 is the number of valid // iterate non-dimensional array to store a value of 0 (active) sparseArr sparse array 35 int count = 0; // * for the first few non-zero data record 36 for (int i = 0; i <11; i ++) {// 11 line 37 for (int j = 0; j <11; j ++ !) {38 if (chessArr1 [ i] [j] = 0) {// the value is not equal to a two-dimensional array COUNT ++ 39 0 ; 40 sparseArr [COUNT] [0] = I; // row 41 sparseArr [count] [ 1] = j; // column 42 sparseArr [count] [2] = chessArr1 [i] [j]; // third column is the value 43 } 44 } 45 } 46 // sparse array of input form 47 the System.out .println (); 48 System.out.println ( "sparse array is obtained ~~~~~" ); 49 for (int I = 0; I <sparseArr.length; I ++) {50 // first row, second column is a column of the third valid value corresponds to a value 51 System.out.printf ( "% d \ t % d \ t% d \ t \ n", sparseArr [i ] [0], sparseArr [I] [1], sparseArr [I] [2 ]); 52 is } 53 is sparse array 54 is 55 // ---> restored to the original two-dimensional array of 56/57 * 1 ** . first read the first line of the sparse array to create an original two-dimensional array based on data from the first row of data such as the above chessArr2 58 * 2. after reading a few lines and sparse array of two-dimensional array can be assigned to the original 59 * / 60 // read the first line 1. the first sparse array to create the original two-dimensional array according to the first line of data such as the above chessArr2 61 / ** sparse array structure is 62 * 1111263 * 121 64 * 2 3 2 65 * / 66 int chessArr2 [] [] = new int [sparseArr [0] [0]] [sparseArr [0] [1]]; // first value a second value row 67 column // 2. in a few lines of data from the second line and assigned to the original two-dimensional array after reading the sparse array 68 // Why start from the second row? Because the first line is the line and the value for 69 (= I int. 1; I <sparseArr.length; I ++ ) {70 chessArr2 [sparseArr [I] [0]] [sparseArr [I] [. 1]] = sparseArr [I] [2 ]; 72 . 73} // 3 outputs two-dimensional array recovery after 74 System.out.println (); 75 System.out.println ( "the restored two-dimensional array" ); 76 77 for (int[] row : chessArr1){ 78 for (int data : row){ 79 System.out.printf("%d\t",data); 80 } 81 System.out.println(); 82 } 83 84 }
Diverted more difficult to understand chessArr2 [sparseArr [i] [0 ]] [sparseArr [i] [1]] = sparseArr [i] [2];
Array Array loop sets the value of which is assigned to a single array sparseArr chessArr2
1 int row=sparsearr[i][0]
2 int col=sparsearr[i][i]
3 chessarr[row][col]=sparsearr[i][2]
4 focuses on the column and row to row and column sparsearr deserves to chessarr
4. The implementation of the results corresponding to