Table like java data structure

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

    Here's one way: make an object called Row to hold each row, and then make a java.util.HashMap whose keys are Integer sij's and whose values are the corresponding Rows.

    public class Example
    {
      public static class Row
      {
        public Integer sij;
        public Integer i;
        public Integer j;
        public Row(Integer sij, Integer i, Integer j)
        {
          this.sij = sij;
          this.i = i;
          this.j = j;
        }
      }
    
      public static void main(String[] args)
      {
        Row r1 = new Row(45, 5, 7);
        Row r2 = new Row(33, 1, 6);
        Row r3 = new Row(31, 0, 9);
        Row r4 = new Row(12, 8, 2);
        Map map = new TreeMap();
        map.put(r1.sij, r1);
        map.put(r2.sij, r2);
        map.put(r3.sij, r3);
        map.put(r4.sij, r4);
        for ( Row row : map.values() ) {
            System.out.println("sij: " + row.sij + " i: " + row.i + " j: " + row.j);
        }
      }
    }
    

    When this runs it produces:

    sij: 12 i: 8 j: 2
    sij: 31 i: 0 j: 9
    sij: 33 i: 1 j: 6
    sij: 45 i: 5 j: 7
    

提交回复
热议问题