亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: 求助:一個(gè)關(guān)于TreeSet的問題,存在困惑! [打印本頁]

作者: aramy    時(shí)間: 2009-08-06 22:34
標(biāo)題: 求助:一個(gè)關(guān)于TreeSet的問題,存在困惑!
問題描述:現(xiàn)有一些客戶數(shù)據(jù)要做統(tǒng)計(jì)篩選,設(shè)計(jì)用TreeSet來裝載數(shù)據(jù)。將客戶數(shù)據(jù)放入樹中,對客戶金額排序,當(dāng)遇到客戶號相同時(shí),插入樹時(shí),將余額相加!代碼如下:
先設(shè)計(jì)一個(gè)節(jié)點(diǎn)類(filenam:TradeNode.java):
import java.util.Comparator;

public class TradeNode implements Comparable<TradeNode> {
    private String cstm; // 客戶號

    private Integer mon = 0; // 交易金額


    public TradeNode(String cstm, int mon) {
        this.mon = mon;
        this.cstm = cstm;
    }

    public int compareTo(TradeNode o) {
        if (o.cstm.equals(this.cstm)) {
            o.mon += this.mon;
            return 0;
        } else if (this.mon == o.mon) {
            return this.cstm.compareTo(o.cstm);
        } else {
            return (o.mon - this.mon);
        }
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub

        return "[" + cstm + "] [" + mon + "]";
    }

    public int getMon() {
        return mon;
    }

    public void setMon(Integer mon) {
        this.mon = mon;
    }

    public String getCstm() {
        return cstm;
    }
}


然后繼承了TreeSet重寫了add函數(shù)(filename:LsTree.java):
import java.util.TreeSet;

public class LsTree extends TreeSet<TradeNode> {
    // 添加子節(jié)點(diǎn)

    public boolean add(TradeNode node) {
        // System.out.println("add node="+node);

        if (contains(node)) {
            return true;
        }
        return super.add(node);
    }
}

測試(filename:testtree2.java:
public class testtree2 {
    public static void main(String[] args) {
        TradeNode nd1 = new TradeNode("44010358010481", 150354);
        TradeNode nd2 = new TradeNode("44010358010481", 150641);
        TradeNode nd3 = new TradeNode("44010358010481", 270000);
        TradeNode nd4 = new TradeNode("44010039275685", 10000);
        TradeNode nd5 = new TradeNode("44010039275685", 980000);
        TradeNode nd6 = new TradeNode("44010039275685", 5000);
        LsTree tree = new LsTree();
        tree.add(nd1);
        tree.add(nd2);
        tree.add(nd3);
        tree.add(nd4);
        tree.add(nd5);
        tree.add(nd6);
        for (TradeNode node : tree) {
            System.out.println(node);
        }
    }

}

得到的結(jié)果:
[44010039275685] [980000]
[44010358010481] [570995]
[44010039275685] [15000]


按我的想法,該樹應(yīng)該只有兩個(gè)節(jié)點(diǎn),可是輸出卻是3個(gè)節(jié)點(diǎn),左思右想不明白,測試數(shù)據(jù)修改后有時(shí)又能得到正確的結(jié)果。剛學(xué)習(xí)java沒多久,請大家?guī)兔纯!謝謝!
作者: aramy    時(shí)間: 2009-08-07 08:55
標(biāo)題: 自己解決!
做測試:
public class testtree {
    public static void main(String[] args) {
        TradeNode nd1 = new TradeNode("A", 100);
        TradeNode nd2 = new TradeNode("B", 10);
        TradeNode nd3 = new TradeNode("B", 1000);
        LsTree tree = new LsTree();
        tree.add(nd1);
        tree.add(nd2);
        tree.add(nd3);
        for (TradeNode node : tree) {
            System.out.println(node);
        }
    }
}
樹是二叉樹,當(dāng)調(diào)用contains方法時(shí),是對余額排序,存在節(jié)點(diǎn)與插入節(jié)點(diǎn)如果分布在樹的兩側(cè),則必然導(dǎo)致,以上結(jié)果。
作者: 一艘小船    時(shí)間: 2009-08-16 13:56
提示: 作者被禁止或刪除 內(nèi)容自動(dòng)屏蔽




歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2