Table like java data structure

前端 未结 7 1793
星月不相逢
星月不相逢 2020-12-03 01:14

I need to implement some kind table-like data structure that stores info like this in Java:

+--------+-------+-----+
|  sij   |   i   |  j  |
+--------+-----         


        
7条回答
  •  星月不相逢
    2020-12-03 01:34

    What do you mean with:

    i have to be able to sort it by the sij parameter

    What's wrong with:

    Object [][] data
    

    EDIT

    Ok, just guessing that what you need is a "StrangeDataStructure" which holds the array, and helps you to sort by the first column, then the only thing that you need is something like this:

    class Structure {
        Object [][] data;
        Object [] indexColumn; // the sij?
    }
    

    And that's it: you should add a sort method indicating the direction, and sort using the "indexColumn"

    It is VEEERY simple I think ( and If I understood your "question" )

    You know what? I'm going to implement it.

    // time elapses...

    Here it is:

    import java.util.Comparator;
    import java.util.Arrays;
    
    public class StrangeStructure {
    
        private Integer [][] data;
        private Integer [] sij; // what is sij anyway?
    
        public StrangeStructure( Integer [][] matrix  ) {
            data = matrix;
            sij = new Integer[ data.length ];
            for( int i = 0 ; i < data.length ; i++ ) {
                sij[i] = data[i][0];
            }
        }
    
        public void sort( Direction direction  ) {
    
            Comparator sijComparator  = new DataComparator( direction, true );
            Comparator dataComparator = new DataComparator( direction, false );
    
            Arrays.sort( sij, sijComparator );
            Arrays.sort( data, dataComparator  );
    
        }
    
        public static void main( String [] args ) {
    
            StrangeStructure s =  
                new StrangeStructure( new Integer[][]{
                                      { 45, 5, 7 }, 
                                      { 33, 1, 6 }, 
                                      { 31, 0, 9 }, 
                                      { 12, 8, 2 }    
                                });
    
            System.out.printf("Original:\n%s", s );       
    
            s.sort( Direction.MIN_TO_MAX );  
            System.out.printf("Min to max:\n%s", s );       
    
            s.sort( Direction.MAX_TO_MIN );  
            System.out.printf("Max to min\n%s", s );       
    
        }
    
    
        public String toString() {
            StringBuilder b = new StringBuilder();
            for( Integer [] row : data ) {
                for( int i : row ) {
                    b.append( i+",");
                }
                b.append("\n");
            }
            return b.toString();
    
        }
    
    }
    class DataComparator implements Comparator {
    
        private Direction direction;
        private boolean isSij;
    
        public DataComparator( Direction d, boolean isSij ) {
            this.direction = d;
            this.isSij = isSij;
        }
    
        public int compare( Object one , Object two  ) {
            if( isSij ){
                return doCompare( direction, (Integer) one, (Integer) two );
            } else {
                return doCompare( direction, ((Integer[])one)[0], ((Integer[])two)[0]);
            }
        }
        public int doCompare( Direction d, int one, int two  ) {
            int a = ( d == Direction.MIN_TO_MAX? one: two );
            int b = ( d == Direction.MIN_TO_MAX? two: one ) ;
            return a - b;
        }
        public boolean equals( Object o ) {
            return false;
        }
    }
    
    
    
    enum Direction{
        MIN_TO_MAX,
        MAX_TO_MIN
    }
    

    Output:

    Original:
    45,5,7,
    33,1,6,
    31,0,9,
    12,8,2,
    Min to max:
    12,8,2,
    31,0,9,
    33,1,6,
    45,5,7,
    Max to min
    45,5,7,
    33,1,6,
    31,0,9,
    12,8,2,
    

提交回复
热议问题