/*
高级for循环是迭代器Collection的缩写。 也就是说,集合中的迭代器可以用高级for替换。
格式:
for )数据类型变量名称:方便的集合(Collection )或数组) )。
{
}
高级for循环仅遍历集合。
只能检索集合元素,但不能操作集合。
迭代器不仅可以进行遍历,还可以对remove集合中的元素执行操作。
也可以使用ListIterator添加或删除集合中的元素。
传统的for循环和高级的for循环有什么区别?
高级for循环是有限的,需要遍历的目标
遍历数组时,建议使用传统的for。 因为传统的for可以定义角标记。
*/
import java.util.*;
class ForEachDemo
{
publicstaticvoidmain (字符串[ ] args ) )。
{
/*
ArrayList al=new ArrayList (;
al.add(ABC1);
al.add(ABC2);
al.add(ABC3);
for(strings:al ) )
sop(s;
int [ ] arr={ 1,3,4,5,2 };
for(intI:ARR ) )。
sop(I: ) I;
*/
HashMap hm=new HashMap (;
hm.put(1,' hello1';
hm.put(3,' hello3';
hm.put(2,' hello2';
hm.put(4,' hello4';
Set set=hm.keySet (;
for(integerI:set ) )。
sop(I'.'hm.get ) I );
Set set1=hm.entrySet (;
for(map.entryme:set1) )。
{
Integer key=me.getKey (;
String value=me.getValue (;
sop (密钥)……) value;
}
}
公共服务语音(objectobj )。
{
system.out.println(obj;
}
}
译文: http://blog.csdn.net/ie MDM 1110/article/details/51371849