当前位置 : 主页 > 编程语言 > c++ >

ArrayList去除从重复元素的应用

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt package day16.ArrayList;import java.util.ArrayList;import java.util.Iterator;/** * 描述: * ArrayList去除从重复元素的应用 * * @outhor 71948 * @create 2017-10-01 15:39 */public class Demo1_ArrayList_New { public stat
gistfile1.txt
package day16.ArrayList;

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

/**
 * 描述:
 * ArrayList去除从重复元素的应用
 *
 * @outhor 71948
 * @create 2017-10-01 15:39
 */
public class Demo1_ArrayList_New {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add("a");
        list.add("a");
        list.add("b");
        list.add("b");

        ArrayList newList = getSingle(list);
        System.out.println(newList);
    }

    public static ArrayList getSingle(ArrayList list){
        ArrayList newList = new ArrayList();
        Iterator it = list.iterator();
        while (it.hasNext()){
            Object obj = it.next();
            if (!newList.contains(obj)){
                newList.add(obj);
            }
        }
        return  newList;
    }
}
网友评论