【去除集合中字符串的重复值-1】

package com.yjf.esupplier.common.test;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author shusheng
 * @description 去除集合中字符串的重复值(字符串的内容相同)
 * @Email [email protected]
 * @date 2018/12/12 16:24
 */
public class ArrayListDemo {
    /**
     * ArrayList 去除集合中字符串的重复值(字符串的内容相同)
     * <p>
     * 分析:
     * A:创建集合对象
     * B:添加多个字符串元素(包含内容相同的)
     * C:创建新集合
     * D:遍历旧集合,获取得到每一个元素
     * E:拿这个元素到新集合去找,看有没有
     * 有:不搭理它
     * 没有:就添加到新集合
     * F:遍历新集合
     */
    public static void main(String[] args) {
        ArrayList array = new ArrayList();
        array.add("hello");
        array.add("world");
        array.add("world");
        array.add("world");
        array.add("java");
        array.add("java");
        array.add("hello");
        array.add("hello");
        array.add("software");

        ArrayList newArray = new ArrayList();

        Iterator it = array.iterator();
        while (it.hasNext()) {
            Object s = it.next();
            if (!newArray.contains(s)) {
                newArray.add(s);
            }
        }

        for (int x = 0; x < newArray.size(); x++) {
            System.out.println(newArray.get(x));
        }
    }

}

猜你喜欢

转载自www.cnblogs.com/zuixinxian/p/10340835.html
今日推荐