遍历集合,可以使用迭代器遍历,也可以使用增强for循环遍历。
@Test
public void test1() {
Collection col = new ArrayList();
col.add(123);
col.add("abc");
col.add(456);
for(Object obj:col) {
System.out.println(obj);
}
}
也可以使用 增强for循环遍历数组,如下所示
@Test
public void test2() {
String[] str = new String[] {"aa", "bb", "cc"};
for(String s:str) {
System.out.println(s);
}
}