JAVA for循环的几种用法

孤街醉人 提交于 2020-12-15 01:44:10

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!