zoukankan      html  css  js  c++  java
  • 【去除集合中字符串的重复值-1】

    package com.yjf.esupplier.common.test;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    
    /**
     * @author shusheng
     * @description 去除集合中字符串的重复值(字符串的内容相同)
     * @Email shusheng@yiji.com
     * @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));
            }
        }
    
    }
    终身学习者
  • 相关阅读:
    数组名与指向数组的指针
    如何实现带可变长参数的函数
    assert()的使用
    参数入栈的顺序以及栈/堆的生长顺序
    指向函数的指针
    各变量入栈顺序
    数组与指针
    C中空指针、NULL与0
    C中为什么不能用==比较字符串?
    在命令行窗口中输入EOF
  • 原文地址:https://www.cnblogs.com/zuixinxian/p/10340835.html
Copyright © 2011-2022 走看看