集合遍历的三种方法
-
转数组,数组遍历循环
-
使用iterator()方法构造迭代器
-
增强for
Collection<String> col = new ArrayList<>();
col.add("theSky");
col.add("Ning");
col.add("Rookie");
col.add("Jakeylove");
col.add("Baolan");
System.out.println(col);
System.out.println("=============1转数组===========");
String[] ary = new String[col.size()];
col.toArray(ary);
//快捷键 ary.fori
for (int i = 0; i < ary.length; i++) {
System.out.println(ary[i]+","+ary[i].toUpperCase());
}
/*快捷键ary.for
for (String s : ary) {
System.out.println(s+","+s.toUpperCase());
}*/
System.out.println("=============2迭代器===========");
Iterator<String> it = col.iterator();
// 快捷键 itit
while (it.hasNext()) {
String next = it.next();
System.out.println(next+","+next.toLowerCase());
}
System.out.println("=============3增强for===========");
for (String s : col) {
System.out.println(s+","+s.toUpperCase());
}