Table like java data structure

前端 未结 7 1781
星月不相逢
星月不相逢 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:52

    Read the section from the Swing tutorial on How to Use Tables. The tutorial shows how to create a table as well as how to add sorting capability to the table.

    If you only need to store the data but not display it, then you can use a 2-dimensional array or a List of Lists. Then you can use the Column Comparator to do the sorting.

    Edit: added code demonstrating use of the ColumnComparator

    import java.util.*;
    
    public class SortSIJ
    {
        public static void main(String args[])
        {
            Object[] data = new Object[4];
            data[0] = new Integer[] {45, 5, 7};
            data[1] = new Integer[] {33, 1, 6};
            data[2] = new Integer[] {31, 0, 9};
            data[3] = new Integer[] {12, 8, 2};
    
            ColumnComparator cc = new ColumnComparator(0);
    //      cc.setAscending( false );
    
            Arrays.sort(data, cc);
    
            for (Object row: data)
            {
                Integer[] theRow = (Integer[])row;
                System.out.println( Arrays.asList(theRow) );
            }
        }
    }
    

    I also agree with the suggestion to create an Object to store the 3 variables. In this case you can use the BeanComparator which can be found at the above link.

提交回复
热议问题