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);
		}
	}
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM