本帖最后由 李挺 于 2013-2-23 20:27 编辑
- package day28;
- import java.lang.reflect.Method;
- import java.util.*;
- class RemoveArrayListTest
- {
- public static void sop(Object obj)
- {
- System.out.println(obj);
- }
-
- public static void main(String[] args) throws Exception, Exception
- {
- ArrayList al = new ArrayList();
- al.add("java01");
- al.add("java02");
- al.add("java03");
- al.add("java01");
- al.add("java03");
- al.add("java02");
- al.add("java05");
- al.add("java04");
- sop("原集合:" + al);
- Method method=RemoveArrayListTest.class.getMethod("singleElement", ArrayList.class);
- al=(ArrayList)method.invoke(new RemoveArrayListTest(), al);
- //al = singleElement(al);
- sop("新集合:" + al);
- }
- public ArrayList singleElement(ArrayList al)
- {
- //定义一个临时容器
- ArrayList newAl = new ArrayList();
- Iterator it = al.listIterator();
- //判断是否有重复值,没有则加入新集合
- while (it.hasNext())
- {
- Object obj = it.next();
- if (!newAl.contains(obj))
- newAl.add(obj);
- }
- return newAl;
- }
- }
复制代码 用反射啊亲! |