<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Sealyu

    --- 博客已遷移至: http://www.sealyu.com/blog

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks
    作者: sealyu   日期:2009-1-8
    在項目中碰到一個bug,拋出ClassCastException異常,找了半天,終于定位問題所在。
    在TreeSet的javadoc里寫到:
    /**
         * Constructs a new, empty set, sorted according to the elements' natural
         * order.  All elements inserted into the set must implement the
         * <tt>Comparable</tt> interface.  Furthermore, all such elements must be
         * <i>mutually comparable</i>: <tt>e1.compareTo(e2)</tt> must not throw a
         * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and
         * <tt>e2</tt> in the set.  If the user attempts to add an element to the
         * set that violates this constraint (for example, the user attempts to
         * add a string element to a set whose elements are integers), the
         * <tt>add(Object)</tt> call will throw a <tt>ClassCastException</tt>.
         *
         * @see Comparable
         */
        public TreeSet() {
        this(new TreeMap<E,Object>());
        }

    也就是說,在使用零參的構造函數時,你所要插入set的elements必須都聲明Comparable接口。
    如果沒有聲明該接口,當你對里面的元素進行排序或者比較操作(所有調用e1.compareTo(e2)的操作),都會拋出一個ClassCastException。同時任何試圖插入沒有聲明該接口的元素也會拋出此異常。
    謹記!

    posted on 2009-01-08 09:40 seal 閱讀(1497) 評論(3)  編輯  收藏 所屬分類: Java基礎

    評論

    # re: TreeSet()零參構造函數引起的問題 2009-01-08 10:12 徐堯
    恩,挺好  回復  更多評論
      

    # re: TreeSet()零參構造函數引起的問題 2009-01-08 12:05 逝水fox
    TreeSet和TreeMap都需要一個排序算法來維護順序 而這個排序算法就必須要由元素自身實現Comparable或者外置的排序算法Comparator來實現了
    不過用的時候 編譯器是沒有什么提示的 這里確實不是很好咯  回復  更多評論
      

    # re: TreeSet()零參構造函數引起的問題 2009-01-11 23:27 愛吃魚頭
    @逝水fox
    學習了,謝謝哦  回復  更多評論
      

    主站蜘蛛池模板: 亚洲精品视频在线| 亚洲欧洲一区二区三区| 亚洲精品福利视频| 中国一级毛片视频免费看| ZZIJZZIJ亚洲日本少妇JIZJIZ| 日韩在线一区二区三区免费视频| 国产又长又粗又爽免费视频| 国产成人亚洲精品蜜芽影院| 亚洲国产精品无码久久青草| 国产精品美女久久久免费| 国产亚洲精品a在线无码| 95老司机免费福利| 波多野结衣亚洲一级| 成人国产mv免费视频| 国产成人综合亚洲| 国产亚洲精品无码拍拍拍色欲| 国产成人无码区免费内射一片色欲| 亚洲AV人人澡人人爽人人夜夜| 最近的中文字幕大全免费8| 亚洲三级高清免费| 亚洲国产一区明星换脸| 99视频在线免费观看| 亚洲欧洲自拍拍偷午夜色| 搡女人免费视频大全| 免费人成视频在线播放| 国产l精品国产亚洲区在线观看| 24小时免费看片| 亚洲色www永久网站| 亚洲AV网站在线观看| 无码人妻精品中文字幕免费 | 亚洲精品无码不卡在线播HE| 99re免费在线视频| 粉色视频成年免费人15次| 亚洲成A人片在线观看无码不卡 | 国产精品另类激情久久久免费| 一个人免费观看视频在线中文| 亚洲处破女AV日韩精品| 在线精品免费视频| 亚洲免费观看视频| 久久亚洲精品高潮综合色a片| 亚洲精品无码成人AAA片|