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