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

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

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

    泰仔在線

    java學習,心情日記,繽紛時刻
    posts - 100, comments - 34, trackbacks - 0, articles - 0

    由List.contains(Object, obj)想到的

    Posted on 2007-01-24 10:03 泰仔在線 閱讀(5647) 評論(1)  編輯  收藏 所屬分類: Java 相關

       昨天在開發的時候,碰到這樣一個問題:我有一個ArrayList<ArtistEntity>的對象artistList,每次在加入對象時,我都對加入的對象artistEntity進行了判斷,如果artistList不包含artistEntity,才進行加入:

    if  ( ! artistList.contains(artistEntity))  {
       artistList.add(artistEntity);
    }

       但運行的結果并不是如開始設計的那樣,頁面顯示表明artistList包含了兩個相同的ArtistEntity對象,這是什么原因呢?
       我看了一下jdk關于這段的源代碼:

         /**
         * Returns <tt>true</tt> if this list contains the specified element.
         *
         * 
    @param  elem element whose presence in this List is to be tested.
         * 
    @return   <code>true</code> if the specified element is present;
         *        <code>false</code> otherwise.
         
    */

        
    public   boolean  contains(Object elem)  {
        
    return  indexOf(elem)  >=   0 ;
        }


        
    /**
         * Searches for the first occurence of the given argument, testing 
         * for equality using the <tt>equals</tt> method. 
         *
         * 
    @param    elem   an object.
         * 
    @return   the index of the first occurrence of the argument in this
         *          list; returns <tt>-1</tt> if the object is not found.
         * 
    @see      Object#equals(Object)
         
    */

        
    public   int  indexOf(Object elem)  {
        
    if  (elem  ==   null {
            
    for  ( int  i  =   0 ; i  <  size; i ++ )
            
    if  (elementData[i] == null )
                
    return  i;
        }
      else   {
            
    for  ( int  i  =   0 ; i  <  size; i ++ )
            
    if  (elem.equals(elementData[i]))
                
    return  i;
        }

        
    return   - 1 ;
        }
       這時會直接調用Object對象的equals進行對象比較,而當此方法被重寫時,通常有必要重寫 hashCode 方法,以維護 hashCode 方法的常規協定,該協定聲明相等對象必須具有相等的哈希碼。而我沒有重寫,因此就導致了這里的問題了。
       修正:直接利用Eclipse重寫,右鍵/源代碼/生成hashCode()和equals(),選擇要包括在hashCode()和equals()方法中的字段:
        /**
         * 
    @return
         * 
    @see java.lang.Object#hashCode()
         
    */

        @Override
        
    public int hashCode() {
            
    final int PRIME = 31;
            
    int result = 1;
            result 
    = PRIME * result + ((artstCd == null? 0 : artstCd.hashCode());
            
    return result;
        }


        
    /**
         * 
    @param obj
         * 
    @return
         * 
    @see java.lang.Object#equals(java.lang.Object)
         
    */

        @Override
        
    public boolean equals(Object obj) {
            
    if (this == obj)
                
    return true;
            
    if (obj == null)
                
    return false;
            
    if (getClass() != obj.getClass())
                
    return false;
            
    final ArtistEntity other = (ArtistEntity) obj;
            
    if (artstCd == null{
                
    if (other.artstCd != null)
                    
    return false;
            }
     else if (!artstCd.equals(other.artstCd))
                
    return false;
            
    return true;
        }
    這樣就好了。
    或者,在加入artistEntity對象,比較artistEntity的key值。我這里是另外用一個ArrayList<String> artistCodeList存放artistEntity的key值,如果artistCodeList不包含待判斷ArtistEntity的key時,才將artistEntity加入artistList:
    // 當アーティスト情報列表中不包含時才加入
                        if (!artistCodeList.contains(artistEntity.getArtstCd())) {
                            artistCodeList.add(artistEntity.getArtstCd());
                            artistList.add(artistEntity);
                        }
     

    Feedback

    # re: 由List.contains(Object, obj)想到的  回復  更多評論   

    2007-02-14 17:51 by sosn
    好好學習天天向上
    主站蜘蛛池模板: 国产精成人品日日拍夜夜免费| 精品一区二区三区无码免费直播| 在线成人精品国产区免费| 亚洲福利在线播放| 一日本道a高清免费播放| 亚洲精品国产高清不卡在线| 免费看黄网站在线看| 亚洲人成网站在线观看青青| 亚洲视频在线免费| 国产亚洲美女精品久久久| 中文字幕版免费电影网站| 久久精品国产精品亚洲精品| 免费人成毛片动漫在线播放| 亚洲国产精品久久久天堂| 久久99精品视免费看| 亚洲天堂中文字幕在线观看| 免费可以在线看A∨网站| 亚洲av无码无线在线观看| 国产99视频精品免费视频7| 国产精品午夜免费观看网站 | 花蝴蝶免费视频在线观看高清版| 亚洲愉拍99热成人精品热久久| APP在线免费观看视频| 亚洲性一级理论片在线观看| 成人午夜大片免费7777| a在线视频免费观看在线视频三区 a毛片成人免费全部播放 | 日日狠狠久久偷偷色综合免费| 亚洲亚洲人成综合网络| 久久久久久AV无码免费网站下载| 亚洲mv国产精品mv日本mv| 四虎影院永久免费观看| 最新国产乱人伦偷精品免费网站 | 国产伦一区二区三区免费| 十八禁视频在线观看免费无码无遮挡骂过| 亚洲精品乱码久久久久久中文字幕| 午夜免费福利小电影| 亚洲最大中文字幕无码网站| 久久亚洲AV无码西西人体| 国内精自视频品线六区免费| 色噜噜噜噜亚洲第一| 亚洲激情中文字幕|