本帖最后由 胡元 于 2012-6-18 07:07 编辑
- import java.util.*;
- class SingleElement
- {
- public static void main(String[] args)
- {
- ArrayList al = new ArrayList();
- al.add("java01");
- al.add("java01");
- al.add("java02");
- al.add("java02");
- al.add("java03");
- al.add("java03");
- sop(al);
- al = singleElement(al);
- sop(al);
- }
- public static ArrayList singleElement(ArrayList al)
- {
- ArrayList ar = new ArrayList();
- Iterator it = al.iterator();
-
- while(it.hasNext())
- {
- Object obj = it.next();
- sop(obj);
- if(!al.contains(obj))//我调试过了,这里出了问题。但是明明和老师讲的一样呀!
- ar.add(obj);
- }
- return ar;
- }
- public static void sop(Object obj)
- {
- System.out.println(obj);
- }
- }
复制代码 傻话不说了,直接上代码! |
|