JAVA for循环的几种用法
1、遍历数组
a、传统方式
1 // String[] strings = {"甲", "乙", "丙", "丁"}; 2 String[] strings = new String[]{"甲", "乙", "丙", "丁"}; 3 for (int i = 0; i < strings.length; i++) { 4 System.out.println(strings[i]); 5 }
b、简单方式
1 String[] strings = new String[]{"甲", "乙", "丙", "丁"}; 2 for (String str : strings) { 3 System.out.println(str); 4 }
2、遍历Collection
a、传统方式
1 String[] strings = {"甲", "乙", "丙", "丁"}; 2 Collection list = Arrays.asList(strings); 3 for (Iterator itr = list.iterator(); itr.hasNext(); ) { 4 Object str = itr.next(); 5 System.out.println(str); 6 }
b、简单方式
1 String[] strings = {"甲", "乙", "丙", "丁"}; 2 Collection list = Arrays.asList(strings); 3 for (Object string : list) { 4 System.out.println(string); 5 }
参考博文:https://blog.csdn.net/qq_36804363/article/details/87539927