2012年8月5日 星期日

關於LinkedList

我用的軟體為Netbeans

老師可以開我的Hw13_2看看

取眾數的指令不是很了解
其中:
private void print2(Map<String, Integer> hm2) {
        Map<String, Integer> hm = MapUtil.sortByValue( hm2, true );
        this.jTextArea1.setText("依數值排序:\n");
        int total = 0, maxv=0;
        boolean start = false;
        LinkedList<String> ll = new LinkedList();
        for (String e : hm.keySet()) {
            if (! start) { //第一次進入
                maxv = hm.get(e);
                start = true;
            }
            if (hm.get(e) == maxv) ll.add(e);
            this.jTextArea1.append(String.format("姓%s的有 %d人\n", e, hm.get(e)));
            total += hm.get(e);
        }// 接著印出總人數及眾數
            if(maxv==1){
                this.jTextArea1.append(String.format("共有%d人,這些姓名的眾數為:%s", total,ll));
            }
            else{
               this.jTextArea1.append(String.format("共有%d人,這些姓名的眾數為:%s", total,ll.clone()));
            }
        }

紅色粗體那一段ll.clone()不知道是甚麼意思
查了API解釋為:  返回此 LinkedList 的淺表副本。
老師能為我解釋嗎
聽說改成ll.toString();也可以不知道為何@@
A:
你可以認為說clone()只是把記憶體位置複製過去

所以資料是共享的

沒有留言:

張貼留言