我是靠谱客的博主 痴情皮卡丘,这篇文章主要介绍增强for循环 1.底层依然是迭代器 2.可遍历各种类型的数据,现在分享给大家,希望可以做个参考。

增强for循环底层依然是迭代器。 因为单步调试,我们是可以进来的。

增强for也是我们在开发中使用最多的一种遍历方式。

可以实现各种数据类型的遍历。

package org.example.testFor;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import java.util.Map;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        // 遍历Set
        Set<Integer> set = Sets.newHashSet(1, 2, 5, 4, 3);
        for (Integer num : set) {
            System.out.println(num);
        }

        // 遍历HashMap
        Map<Integer, String> map = Maps.newHashMap();
        map.put(1, "123");
        map.put(2, "456");
        for (Integer key : map.keySet()) {
            System.out.println(key + ":" + map.get(key));
        }
    }
}

/*
1
2
3
4
5
1:123
2:456
 */

最后

以上就是痴情皮卡丘最近收集整理的关于增强for循环 1.底层依然是迭代器 2.可遍历各种类型的数据的全部内容,更多相关增强for循环内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(179)

评论列表共有 0 条评论

立即
投稿
返回
顶部