<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视频精品免费专区| 国产成人精品久久亚洲高清不卡 | 欧美亚洲国产SUV| 国产在线19禁免费观看国产| 亚洲综合一区二区三区四区五区| 黄页网站在线看免费| 自拍日韩亚洲一区在线| 最近最好的中文字幕2019免费| 亚洲综合小说另类图片动图| 成年女人免费碰碰视频| 午夜亚洲WWW湿好爽| 免费国产在线观看| 国产99久久久久久免费看| 亚洲人成网站在线观看播放| 一级毛片全部免费播放| 亚洲av无码一区二区三区天堂古代| 野花高清在线观看免费3中文| 中文字幕乱码亚洲精品一区 | 午夜精品免费在线观看 | 亚洲欧美国产欧美色欲| 免费精品国产自产拍观看| 亚洲精品偷拍视频免费观看| 亚洲av无码潮喷在线观看 | 国产妇乱子伦视频免费| 亚洲最大av资源站无码av网址| 丁香亚洲综合五月天婷婷| 国产精品午夜免费观看网站 | 亚洲精品伦理熟女国产一区二区 | 亚洲人JIZZ日本人| 精品国产无限资源免费观看| 午夜亚洲国产理论片二级港台二级 | 最近中文字幕电影大全免费版| 亚洲一级毛片免费在线观看| 日本黄色免费观看| 国内少妇偷人精品视频免费| 亚洲13又紧又嫩又水多| 亚洲综合色视频在线观看| 在线观看H网址免费入口| 一道本不卡免费视频|