您的位置:首页 > 其它

Iterator 关键字,用于集合的遍历

2016-01-01 16:47 260 查看
Iterator  关键字,用于集合的遍历

import java.util.*;
public class IteratorKeyWord {
public static void main(String[] args) {
Collection c = new ArrayList<Object>();
c.add("hello");
c.add(new Integer(5));
System.out.println(c.size());
System.out.println(c);

Iterator i = c.iterator();
while (i.hasNext()) {
Object s = (Object)i.next();
if (s instanceof String) {
String str = (String)s;
System.out.println("i.next is String = " + str);
}else {
System.out.println("i.next is Interger = " + s.toString());
}

}

c.remove("hello");
for (Object o : c ) {
System.out.println(o);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: