First, why use a sparse array?
For example, this board, if you want to record the position of the black and blue pieces of first think of using a two-dimensional array, we built the two-dimensional array (1 black, 2 blue) and found a lot of empty space wasting a lot of memory space
We can use this time to store a sparse array " valid data "
Second, how to use sparse arrays
A total of three sparse array (fixed column), respectively the row number, column number and value
The first line: the initial array of rows, columns, RMS (meaningful values)
Other lines: Successive recording of rms line number, column number and value
The figure shows that the number of rows of the sparse array RMS (effective value assumed num) num + 1, column number 3
Third, code implementation
Sparse array turn ideas:
- Define a good array, and assign
- Through the array to get the number of valid num
- Defines the sparse array, the number of lines num + 1, the number of columns 3, the first row of the array Assignment
- Again initial traverse an array, to obtain a valid value assigned to the sparse array
- Definition is complete
Sparse array to a two-dimensional array:
- Define a two-dimensional array of rows and columns of the first row is read from the sparse array
- Setting cycle, according to a few lines later to restore the sparse array
Code:
1 int [] [] = array1 new new int [. 11] [. 11 ]; 2 // array assigned a black, blue 2 . 3 array1 [1] [2] = 1 ; . 4 array1 [2] [. 3 ] = 2 ; 5 // iterate . 6 for ( int [] Row: array1) { . 7 for ( int Item: Row) { . 8 of System.out.print (Item + "\ T" ); . 9 } 10 the System.out. the println (); . 11 } 12 is int NUM = 0 ; 13 is // iterate again, to find the actual existence of a few number of 14 for ( int I = 0; I <. 11; I ++ ) { 15 for ( int J = 0; J <. 11; J ++ ) { 16 IF (array1 [I] [J] = 0! {) . 17 NUM ++ ; 18 is } . 19 } 20 is } 21 is System.out.println ( "NUM =" + NUM); 22 is 23 is // begin defining the sparse array 24 int [] [] = Sparse_array new new int [NUM +. 1] [. 3 ]; 25 Sparse_array [0] [0] =. 11 ; 26 is Sparse_array [0] [. 1] =. 11 ; 27 Sparse_array [0] [2] =NUM; 28 29 // again traverse the sparse array assignment 30 int COUNT = 0 ; 31 is for ( int I = 0; I <. 11; I ++ ) { 32 for ( int J = 0; J <. 11; J ++ ) { 33 is IF (! array1 [I] [J] = 0 ) { 34 is COUNT ++ ; 35 Sparse_array [COUNT] [0] = I; 36 Sparse_array [COUNT] [. 1] = J; 37 [Sparse_array [COUNT] [2] = array1 [I ] [J]; 38 is } 39 } 40 } 41 is 42 // output sparse array 43 System.out.println ( "+++++++++++++++++++++ sparse array +++++++++++++ + " ); 44 is for ( int I = 0; I <Sparse_array.length; I ++ ) { 45 System.out.printf ("% D \ D% T \ T% D \ T \ n-", Sparse_array [I] [ 0], Sparse_array [I] [. 1], Sparse_array [I] [2 ]); 46 is } 47 System.out.println (); 48 49 // sparse array recovery 50 int [] [] = array2 new new int [Sparse_array [0] [0]] [Sparse_array [0] [. 1 ]]; 51 is // sparse array and assign loop 52 is for ( int. 1 = I; I <Sparse_array.length; I ++) { 53 is array2 [Sparse_array [I] [0]] [Sparse_array [I] [. 1]] = Sparse_array [I] [2 ]; 54 is } 55 56 is // displayed recovered array 57 System.out.println ( "+ +++++++++++++++++ recovered array +++++++++++++++ " ); 58 for ( int [] Row: array2) { 59 for ( int Item: Row) { 60 System.out.printf (Item + "\ T" ); 61 is } 62 is System.out.println (); 63 is } 64 65
About Assignment understood as a sparse array:
count++;
Sparse_array[count][0]=i;
Sparse_array[count][1]=j;
Sparse_array[count][2]=array1[i][j];
Why count?
We are a progressive assignment cycle, when his party assignment, will become only the column, so the definition of a count locking row
After reading the article is Shangxue Tang teacher after class Hanshun Ping sparse array to write, write down hopes to strengthen at the memory