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