Data structures generally divided into linear and nonlinear structural configuration
If subdivided nonlinear structure can be divided into FIGS and trees, multi-dimensional arrays (or more two-dimensional) , plus a set of (typically not study)
Linear structure
1. The data elements exist between one to one relationship
2. Data stored in two ways, is sequentially stored (one-dimensional array ) and a chain storage (list),
Wherein sequentially stored table is called a linear, continuous, linked storage element is called the list of physical storage, physically necessarily continuous
3. common linear structure
Arrays, linked lists, stacks, queues
Array
Sparse array: When a majority of the array of values which are the same , they can use the sparse array (3 columns will always be)
Recording only the line where the value of the column, and a value (to a smaller memory array), and the number of different sizes and values of the original array
Such as backgammon
The left is the right of the normal array is sparse arrays, meaning the right
Red frame memory size of the original array and the upper side has a different number of values below the 0 line represents red frame 2 has a data value of 1 0 Similarly, line 3 has a value of 2 data........
Such storage capacity is less than the left
Turn the two-dimensional sparse
Obtaining the current number of different values (traversing) n
Create a sparse array int [n + 1] [3]
Saving data, the first row must be a two-dimensional array of rows, columns, n-
Sparse turn-D
To read the first row, obtaining n rows, m columns to create a two-dimensional array of values int [n] [m]
Read back data
public class MainTest { public static void main (String [] args) { // direct assignment int [] [] of arr1 = {{0,0,0,0,0}, {1,0,4,0,51} , {0,0,0,0,0}, {0,0,0,0,0}, {0,0,0,0,0 }}; // Create an object / * int [] [] arr2 = new int [5] [ 5]; arr2[1][0]=1; arr2[1][2]=4; arr2 is [. 1] [. 4] = 51 is; * / // print of arr1 System.out.println ( "two-dimensional array:" ); for ( int [] Row: of arr1) { for ( int I: Row) { System.out.print(i); System.out.print(" "); } System.out.println(); } //遍历arr1二维数组 int count=0; for(int i=0;i<arr1.length;i++){ for(int j=0;j<arr1[i].length;j++){ if(arr1[i][j]!=0) count++; } } // Create the number of sparse array acts + 1'd int [] [] = arrmix new new int [COUNT +. 1] [. 3 ]; arrmix[0][0]=5; arrmix[0][1]=5; arrmix[0][2]=count; // the value is stored in a two-dimensional array // sparse array row 0 through row data has been saved, and starting from the first row int n-=. 1 ; for ( int I = 0; I <arr1.length; I ++ ) { for ( int J = 0; J <of arr1 [I] .length; J ++ ) { IF (of arr1 [I] [J] = 0! ) { arrmix[n][0] = i; arrmix [n] [ 1] = j; arrmix[n][2] = arr1[i][j]; n++; } } } // print the sparse array System.out.println ( "sparse array:" ); / * System.out.println (arrmix [0] [0]); System.out.println(arrmix[0][1]); System.out.println(arrmix[0][2]);*/ for(int[] row:arrmix) { for (int i : row) { System.out.print(i); System.out.print(" "); } System.out.println(); } // restore the two-dimensional array int [] [] = arr2 is new new int [arrmix [0] [0]] [arrmix [0] [. 1 ]]; for ( int I =. 1; I <+ COUNT. 1; I ++ ) { arr2[arrmix[i][0]][arrmix[i][1]]=arrmix[i][2]; } System.out.println ( "Recovery after printing" ); for ( int [] Row: arr2 is) { for ( int I: Row) { System.out.print(i); System.out.print(" "); } System.out.println(); } } }
Print results
------------------------------------------ To be continued, continue to add - -------------------------------------------------- -----------