有一个数据表或数据结构
字段名                      g1                       g2                      g3
1                       中国                        null                    null
2                       美国                        null                      null
3                       法国                        null                      null
4                       中国                        广东省                        null
5                       中国                        吉林省                        null
6                       中国                        广东省                        深圳
7                       中国                        广东省                        东莞
8                       中国                        吉林省                        长春把这样的数据结构转换为id     parent_id   name1      0           中国
2      0           美国
3      0           法国
4      1           广东省
5      1           吉林省
6      4           深圳
7      4           东莞
8      5           长春
这样的数据结构???高人指点。。

解决方案 »

  1.   

    hashmap a
    int index = 1;
    needBean
    list
    for(...){
        if(g2==null&&g3==null){
            a.put(g1, index);
            new needBean(index++, 0, g1);
            list.add(needBean);
        }else if(g3==null){
            addProvince(g1,g2);
        }else{
            if(a.get(g2)==null){
                addProvince(g1,g2);
                new needBean(index++, a.get(g2), g3);
                list.add(needBean);
            }else{
                new needBean(index++, a.get(g2), g3);
                list.add(needBean);
            }
        }
    }addProvince(g1,g2){
         if(a.get(g1)==null){
                a.put(g1, index);
                new needBean(index++, 0, g1);
                list.add(needBean);
                a.put(g2, index);
                new needBean(index++, a.get(g1), g2);
                list.add(needBean);
            }else{
                new needBean(index++, a.get(g1), g2);
                list.add(needBean);
            }
    }
      

  2.   

    先从第一张表总结出两种关系逻辑
    行政区 <-> ID
    行政区 <-> 父行政区
    然后 行政区 <-> 父行政区ID 直接就出来了
      

  3.   

    [QUIT]1      0           中国 
    2      0           美国 
    3      0           法国 
    4      1           广东省 
    5      1           吉林省 
    6      4           深圳 
    7      4           东莞 
    8      5           长春 [/QUIT]
    从上面的表中可以看出,中国 用1就可以表示  中国-广东省中的中国部分可以用1表示  整个中国广东 用4表示
    形象点就是父子孙...的关系了