楼主的代码并没有调用ArrayList singleElement方法,修改如下:
- package com.sung.test;
- import java.util.*;
- class Test04{
- public static void show(Object obj){
- System.out.println(obj);
- }
- public static void main(String[] args){
- ArrayList al = new ArrayList();
- al.add("java01");
- al.add("java02");
- al.add("java03");
- al.add("java01");
- ArrayList a2=singleElement(al);
- show(a2);
- }
-
- public static ArrayList singleElement(ArrayList al){
- ArrayList newal = new ArrayList();
- Iterator it = al.iterator();
- while(it.hasNext()){
- Object obj = it.next();
- if(!(newal.contains(obj))){
- newal.add(obj);
- }
- }
- return newal;
- }
- }
复制代码 代码运行结果为:[java01, java02, java03]
|