Java 数组去重

来源:互联网 发布:python多线程编程实例 编辑:程序博客网 时间:2024/05/01 14:30
import java.util.ArrayList;import java.util.List;public class ListRemoveRepeat {    // 数组去重    public static List<Integer> single(List<Integer> sourceList) {        // 创建存放结果集的集合        List<Integer> resultList = new ArrayList<Integer>();        // 循环数据源        for (int a : sourceList) {            // 如果结果集中存在sourceList中的元素,则将其加入结果集中,已经存在的不加入            if (!resultList.contains(a)) {                resultList.add(a);            }        }        return resultList;    }    public static void main(String[] args) {        // 数据源集合        List<Integer> sourceList = new ArrayList<Integer>();        // 向需要操作的集合中添加数据        sourceList.add(1);        sourceList.add(1);        sourceList.add(2);        sourceList.add(2);        sourceList.add(3);        sourceList.add(3);        List<Integer> result = ListRemoveRepeat.single(sourceList);        for (int a : result) {            System.out.println(a);        }    }}
0 0