隨筆-61  評(píng)論-159  文章-0  trackbacks-0
                  最近重構(gòu)項(xiàng)目里面的一個(gè)功能,其中用到Vector容器,有一種情況是Vector里面含有相同的元素,造成得到的結(jié)果不是預(yù)期的,所以要去掉Vector里面的重復(fù)的元素。
                  通過查看jdk文檔,得知有個(gè)contains()方法,如果此向量包含指定的元素,則返回 true。更確切地講,當(dāng)且僅當(dāng)此向量至少包含一個(gè)滿足 (o==null ? e==null : o.equals(e)) 的元素 e 時(shí),返回 true。
                  JDK原文:

          contains

          public boolean contains(Object elem)
          Tests if the specified object is a component in this vector.

           

          Specified by:
          contains in interface Collection<E>
          Specified by:
          contains in interface List<E>
          Overrides:
          contains in class AbstractCollection<E>
          Parameters:
          elem - an object.
          Returns:
          true if and only if the specified object is the same as a component in this vector, as determined by the equals method; false otherwise.
           
          因此:可以通過該方法來實(shí)現(xiàn)過濾重復(fù)的元素。

          contains方法JDK源碼:

           

          1public boolean contains(Object elem) {
          2    return indexOf(elem, 0>= 0;
          3    }

           

           1public synchronized int indexOf(Object elem, int index) {
           2    if (elem == null{
           3        for (int i = index ; i < elementCount ; i++)
           4        if (elementData[i]==null)
           5            return i;
           6    }
           else {
           7        for (int i = index ; i < elementCount ; i++)
           8        if (elem.equals(elementData[i]))
           9            return i;
          10    }

          11    return -1;
          12    }

          注:contains方法里面返回的indexOf(Object elem, int index)方法,十分重要。

          測試?yán)樱?br />

           1package org.apple.collection.test;
           2
           3import java.util.Vector;
           4
           5public class VectorTest {
           6
           7    /**
           8     * @param args
           9     */

          10    public static void main(String[] args) {
          11        // TODO Auto-generated method stub
          12        Vector<String> v = new Vector<String>();
          13        Vector<String> o = new Vector<String>();
          14        v.add("aaaaa");
          15        v.add("bbbbb");
          16        v.add("aaaaa");
          17        v.add("ccccc");
          18        for(int i=0;i<v.size();i++)
          19        {
          20        if(!o.contains(v.get(i)))
          21            o.add(v.get(i));
          22        }

          23        for(int j = 0;j<o.size();j++)
          24        {
          25            System.out.println(o.get(j));
          26        }

          27
          28    }

          29
          30}

          31

          輸出結(jié)果aaaaa bbbbb  ccccc

          PS:所以通過contains方法可以把重復(fù)元素過濾掉。

           




          -------------------------------------------------------------------------------------------------
          PS:本博客文章,如果沒有注明是有“轉(zhuǎn)”字樣,屬于本人原創(chuàng)。如果需要轉(zhuǎn)載,務(wù)必注明作者文章的詳細(xì)出處地址,否則不允許轉(zhuǎn)載,多謝合作!
          posted on 2008-11-04 23:48 apple0668 閱讀(3983) 評(píng)論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 庆城县| 白城市| 新昌县| 克拉玛依市| 井冈山市| 巴彦淖尔市| 英吉沙县| 广平县| 临清市| 千阳县| 康平县| 疏勒县| 略阳县| 祁东县| 礼泉县| 广西| 娱乐| 赤峰市| 资溪县| 瑞安市| 龙海市| 杂多县| 西吉县| 郁南县| 东源县| 大埔区| 灵台县| 天祝| 庆云县| 长岛县| 五台县| 高青县| 安乡县| 濮阳县| 彰化市| 海安县| 六安市| 仁寿县| 廊坊市| 上栗县| 朝阳县|