本帖最后由 伍淑江 于 2013-3-19 20:45 编辑
- array2并不是对象呀,它为什么能调研Iterator方法的呢?
复制代码 public class ArrayListTest {
public static void main(String[] args) {
ArrayList array = new ArrayList();
array.add("hello");
array.add("world");
array.add("java");
array.add("hello");
ArrayList array2 = getSingleton(array);
Iterator it = array2.iterator(); //这里的array2为什么可以调用iterator( )呀?
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
}
}
public static ArrayList getSingleton(ArrayList array) {
ArrayList arr = new ArrayList();
Iterator it = array.iterator();
while (it.hasNext()) {
String s = (String) it.next();
if (!arr.contains(s)) {
arr.add(s);
}
}
return arr;
}
}
|