1,Iteration 接口
collection 支持iteration()方法,该方法返回一个Iteration的对象。
boolean hasNext(); 判断集合中是否还有下一个元素;
Object next(), 取下一个元素;
void remove() 删除元素;
eg:
Collection c =new ArrayList();
c.add(1);
Iterator i = c.iterator();
Object o =null;
while(i.hasNext){
o = i,next();
}
2,for循环
for(String s:list){
System.out.print(s +" ");
}
3,Enumeration
Collection c =new ArrayList();
c.add(1);
Enumeration e = c.elements();while(e.hasMoreElements()){
Object o = e.nextElement();
}
这个没有remove()方法。
Map迭代
Map map = new HashMap()
map.put(1,"hello");
//返回所有键
Set key = map.keySet();
for(Object o : key){
Object v = map.get(o);
}
//返回所有值
Collection c=map.values();
Iterator ii = c.iterator();
while(ii.hasNext()){
object v = ii.next();
}
//整体迭代
Set s = map.entrySet();//将key和value一起返回
Iterator is= s.iterator();
while(is.hasNext()){
Map.Entry o = (Map.Entry)is.next();
System.out.println(o.getKey()+","+o.getValue());
}