兄弟,我就是要用XML,可是得先把它这样排好了再用呀.

解决方案 »

  1.   

    可以用
    Vector
    或者
    Hashtable
      

  2.   

    我要的是算法,大哥,你这样空说说有什么用,难道我不知道可以用Vector或Hashtable
      

  3.   

    看看这样行吗?
    import java.util.*;class node implements Comparable{
    String pid;
    String cid;
    boolean visit; node(String p, String c) {
    pid = p;
    cid = c;
    visit = false;
    }

    public int compareTo(Object rv) {

    node v = (node)rv;

    if(pid.compareTo(v.pid) > 0) return 1;
    else if(pid.compareTo(v.pid) < 0) return -1;
    else {
    if(cid.compareTo(v.cid) > 0) return 1;
    else if(cid.compareTo(v.cid) < 0) return -1;
    else return 0;
    }
    }
    }public class Tree {
    public static void main(String[] args) {
    node[] nodes = {new node("Root", "PL"), new node("PL", "PL-1"), new node("PL-1", "PL-1-1"),
    new node("PL", "PL-2"), new node("PM", "PM-1"), new node("Root", "PM"),
    new node("PL-1-1", "PL-1-1-1"),new node("PM-2", "PM-2-1"), new node("PM", "PM-2")};


    Object[] roots;
    HashSet hs1= new HashSet();
    HashSet hs2= new HashSet();
    for(int i = 0; i < nodes.length; i++) {
    hs1.add(nodes[i].pid);
    hs2.add(nodes[i].cid);
    }
    hs1.removeAll(hs2);
    System.out.println(hs1);
    roots = hs1.toArray();

    Tree t = new Tree();
    for(int i = 0; i < roots.length; i++) {
    node x = t.find(nodes, (String)roots[i]);
    if (x!=null)
    t.visit(nodes, x, 0);
    else 
    System.out.println("End");
    }
    }

    void visit(node[] nodes, node root, int blank) {
    root.visit = true;
    while(true) {
    print(blank);
    System.out.println(root.pid + "\t" + root.cid);

    node x = find(nodes, root.cid);
    if(x != null) {

    visit(nodes, x, blank+4);
    }

    x = find(nodes, root.pid);
    if(x != null) {
    visit(nodes, x, blank);
    } else  {
    break;
    }
    }

    }

    void print(int n) {
    for(int i = 0; i < n; i++)
    System.out.print(" ");
    }

    node find(node[] nodes, String str) {
    int i;
    for(i = 0; i < nodes.length; i++) {
    if(nodes[i].visit == false && nodes[i].pid.equals(str))
    break;
    }

    if(i < nodes.length) return nodes[i];
    else return null;
    }

    }