本帖最后由 曹德君 于 2013-4-25 10:15 编辑
- import java.util.*;
- class SingalElementDemo
- {
- public static void sop(Object obj)
- {
- System.out.println(obj);
- }
- public static void main(String[] args)
- {
- List al = new ArrayList();//建立父类引用的ArrayList
- al.add("java01");
- al.add("java02");
- al.add("java03");
- al.add("java04");
- al.add("java04");
- al.add("java04");
- sop(al);
- al=singalElement(al);
- sop(al);
- }
-
- public static List singalElement(ArrayList al)//传参如果用的List类就能通过。为什么传参是ArrayList子类,就报错呢不能编译通过呢?
- {
- ArrayList newal = new ArrayList();
- for (Iterator it = al.iterator();it.hasNext(); )
- {
- //
- Object obj = it.next() ;
- if (!newal.contains(obj))
- {
- newal.add(obj);
- }
- }
- return newal;
-
- }
-
- }
复制代码 |