本帖最后由 杨兴庭 于 2013-8-5 18:32 编辑
- package Collection;
- import java.util.*;
- public class ArrayListTest {
- /* 去除ArrayList集合中的重复元素、
- */
- public static void main(String[] args) {
- ArrayList al = new ArrayList();
- al.add("java001");
- al.add("java002");
- al.add("java001");
- al.add("java002");
- al.add("java003");
- sop(al);
- al = singleElement(al);
- sop(al);
- }
- public static void sop(Object obj) {
- System.out.println(obj);
- }
- public static ArrayList singleElement(ArrayList al) {
- // 定义一个临时容器
- ArrayList newAl = new ArrayList();
-
- [color=Red]Iterator it = al.iterator();//这里为什么是al.iterator 而不是newAl.iterator[/color] while (it.hasNext()) {
- Object obj = it.next();
- if (!newAl.contains(obj))
- newAl.add(obj);
- }
- return newAl;
- }
- }
复制代码 请大家帮帮忙,谢谢啦! |