An array of structures and algorithms sparse arrays -1

 

1 sparse array

Use: When most of the elements in an array is 0, or a value of the same array, the sparse array can be used to hold the array 
processing method sparse array: 
an array of records, a total of several odd row, the number of different value of 
two elements having different values and the value recorded in the row of an array of small, thereby reducing the size of the program
// Take backgammon, for example, to create an original two-dimensional array of 11 * 11
         // 0 indicates no pawn, 1 sunspots, 2 albino 
        int [] [] = chessArr1 new new  int [ 11 ] [ 11 ]; 
        chessArr1 [ 1 ] [ 2 ] = . 1 ; 
        chessArr1 [ 2 ] [ . 3 ] = 2 ; 
        chessArr1 [ . 3 ] [ . 3 ] = . 1 ; 
        chessArr1 [ . 4 ] [ . 4 ] = 2 ;
         // get the original two-dimensional array 
        for ( int[] row : chessArr1) {
            for(int data:row) {
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }

Two-dimensional array to a sparse array

Traversing the original two-dimensional array, the number of valid data obtained sum
can be created according to wash sum array sparseArr int [sum + 1] [ 3];
the valid data into two-dimensional array is sparse array

// The two-dimensional array into a sparse array
         // 1. The number of two-dimensional array to traverse, to give non-zero data 
        int SUM = 0 ;
         for ( int I = 0 ; I <chessArr1.length; I ++ ) {
             for ( int J = 0 ; J <chessArr1.length; J ++ ) {
                 IF (chessArr1 [I] [J] =! 0 ) { 
                    SUM ++ ; 
                } 
            } 
        } 
        // 2. Create a sparse array corresponding to the 
        int [] [] sparseArr = new new  int [SUM + . 1 ] [3 ];
         // to sparse array assignment 
        sparseArr [ 0 ] [ 0 ] = . 11 ; 
        sparseArr [ 0 ] [ . 1 ] = . 11 ; 
        sparseArr [ 0 ] [ 2 ] = SUM;
         // traverse the two-dimensional array, a non-zero values stored in the sparse array 
        int COUNT = 0 ;
         for ( int I = 0 ; I <chessArr1.length; I ++ ) {
             for ( int J = 0 ; J <chessArr1.length; J ++ ) {
                IF (! chessArr1 [I] [J] = 0 ) { 
                    COUNT ++ ; 
                    sparseArr [COUNT] [ 0 ] = I; 
                    sparseArr [COUNT] [ . 1 ] = J; 
                    sparseArr [COUNT] [ 2 ] = chessArr1 [I] [J]; 
                } 
            } 
        } 
        // iterate sparse array of 
        the System. OUT .println ( " sparse array is obtained: " );
         for ( int I = 0 ; I <sparseArr.length; I ++ ) {
            for(int j = 0;j < 3;j++) {
                System.out.printf("%d\t",sparseArr[i][j]);
            }
            System.out.println();
        }

Sparse array to a two-dimensional array
to read the first line of the sparse array, the first row of the data, creating [rows] of the original two-dimensional array chessArr2 = int [column number]
re-read data lines of the array after the wash and assigned to the original two-dimensional array

     // restore the sparse array to the original two-dimensional array
         // 1 first reads the first row of the sparse array, according to the data of the first row, to create the original two-dimensional array 
        int [] [] = chessArr2 new new  int [sparseArr [ 0 ] [ 0 ]] [sparseArr [ 0 ] [ . 1 ]];
         // 2. after the re-read data lines assigned to the sparse array to create two-dimensional array 
        for ( int I = . 1 ; I <sparseArr.length; ++ I ) { 
            chessArr2 [sparseArr [I] [ 0 ]] [sparseArr [I] [ . 1 ]] = sparseArr [I] [ 2 ]; 
        } 
        // outputting the restored two-dimensional array 
        . the System OUT .println ( "Press sparse array recovered from two-dimensional array " );
         for ( int [] Row: chessArr2) {
             for ( int Data: Row) { 
                . The System OUT .printf ( " % D \ T " , Data); 
            } 
            . The System OUT . the println (); 
        }

 The array to a file, and then read from the file

     // The two-dimensional array into a sparse array directly read a.txt file 
        File File = new new File ( " a.txt " ); 
        FileWriter fw = new new FileWriter (File);
         for ( int i = 0 ; i <sparseArr .length; I ++ ) {
             for ( int J = 0 ; J < . 3 ; J ++ ) { 
                fw.write (sparseArr [I] [J] + " \ T " ); 
            } 
            fw.write ( " \ n- " );  
        }
        FW .close ();
        // The two-dimensional array a.txt file read out 
        System. OUT .println ( " data obtained from the file !!! " ); 
        BufferedReader br = new new BufferedReader ( new new FileReader ( " a.txt " )) ;
         int [] [] chessArr3 = null ; 
        String line; // row 
        int Z = 0 ;
         the while (! (= br.readLine line ()) = null ) { 
            Z ++ ; 
            String [] TEMP = line.split ( " \ t ");
            if(z==1) {
                chessArr3 = new int[Integer.valueOf(temp[0])][Integer.valueOf(temp[1])];
            }
            if(z>1) {
                for(int i = 0;i < temp.length;i++) {
                    chessArr3[Integer.valueOf(temp[0])][Integer.valueOf(temp[1])]=Integer.valueOf(temp[2]);
                }
            }
        }
        for(int[] row : chessArr3) {
            for(int data:row) {
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }
 }

Guess you like

Origin www.cnblogs.com/bai3535/p/12088702.html