JavaScript中的for in 循环
此处x代表person的属性
var person= {name:"zhangsan",age:"20"};
for (x in person){
alert(x);
txt=txt + person[x];
}
此处x代表people的下标
var person= [{name:"zhangsan",age:"20"},{name:"lisi",age:"21"}];
for (x in person){
alert(x);
txt=txt + person[x];
}
Java中的增强for循环
public class Test {
public static void main(String args[]){
//循环中都是int
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ){
System.out.print( x );
System.out.print(",");
}
System.out.print("\n");
//循环中都是string
String [] names ={"James", "Larry", "Tom", "Lacy"};
for( String name : names ) {
System.out.print( name );
System.out.print(",");
}
}
}
编译结果
10,20,30,40,50,
James,Larry,Tom,Lacy,
当循环内容为集合对象时
public class Test{
public static void main(String args[]){
HashMap<String,String> hashMap = new HashMap<String, String>();
map.put("01","张三");
map.put("02","李四");
map.put("03","王五");
map.put("04","赵六");
Set<Map.Entry<String, String>> entrys = map.entrySet();
for(Map.Entry<String, String> entry :entrys){
System.out.println("键:"+ entry.getKey()+" 值:"+ entry.getValue());
}
}