Java5中的For/in循环
来一个测试的JavaBean:
for/in循环也可以迭代一个数组:
package com.kay.java5.test1;
public class TestBean {
private String name;
private String address;
public TestBean(String name, String address) {
super();
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
使用for/in循环迭代这个集合:public class TestBean {
private String name;
private String address;
public TestBean(String name, String address) {
super();
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
//For/in循环 迭代集合
List<TestBean> list = new ArrayList<TestBean>();
TestBean tb1 = new TestBean("aa","China");
TestBean tb2 = new TestBean("bb","Usa");
list.add(tb2);
list.add(tb1);
for(TestBean tb : list){
System.out.println(tb.getAddress());
}
List<TestBean> list = new ArrayList<TestBean>();
TestBean tb1 = new TestBean("aa","China");
TestBean tb2 = new TestBean("bb","Usa");
list.add(tb2);
list.add(tb1);
for(TestBean tb : list){
System.out.println(tb.getAddress());
}
for/in循环也可以迭代一个数组:
//fro/in循环迭代数组
int[] a = {1,2,3,4,5,6,7,8};
for(int i : a){
System.out.println(i);
}
int[] a = {1,2,3,4,5,6,7,8};
for(int i : a){
System.out.println(i);
}