算法之去掉vetor集合中的重复元素

public class DropRepetition {
    public static void main(String[] args) {
        Vector<String> v = new Vector<String>();
        v.add("1");
        v.add("1");
        v.add("2");
        v.add("2");
        v.add("3");
        v.add("3");
        dropRepetition1(v);
        dropRepetition2(v);
    }

    private static Vector<String> dropRepetition1(Vector<String> v) {
        //创建新的Vector对象
        Vector<String> newV = new Vector<String>();
        for (String str : newV) {
            if(!newV.contains(str))
                newV.add(str);
        }
        return newV;
    }
    private static HashSet<String> dropRepetition2(Vector<String> v) {
        //构造新的HashSet
        HashSet<String> set = new HashSet<String>(v);
        return set;
    }
}

 

posted @ 2017-07-02 19:49  如果屈原会编程  阅读(218)  评论(0编辑  收藏  举报