Java 数组去重

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);
		}
	}
}
posted @ 2017-01-18 22:45  CodingNormal  阅读(1797)  评论(0)    收藏  举报