我是靠谱客的博主 高兴小虾米,这篇文章主要介绍增强型for循环,现在分享给大家,希望可以做个参考。

1)增强型for定义和使用
//for循环
for(int i = 0 ; i<arr.length ; i++){
System.out.println(“元素:”+ arr[i]);
}
//增强型for定义
for(ElementType element: arrayName)
{ //集合或数组的数据类型 变量名:集合名/数组名
System.out.println(变量名);
};
//增强型举例
for(int item :arr){
System.out.println(“元素:”+ item);
}
//增强型举例
for(String item : set){
System.out.println(“元素:”+ item);
}
2)内部原理其实是一个Iterator迭代器,所以在遍历的过程中,不能对集合中的元素进行操作
HashSet set = new HashSet();
//添加元素
set.add(“张狗蛋”);
set.add(“张全蛋”);
set.add(“张傻蛋”);
//使用迭代器遍历Set的集合.
Iterator it = set.iterator();
while(it.hasNext()){
String temp = it.next();
System.out.println(“元素:”+ temp);
it.remove();
}

最后

以上就是高兴小虾米最近收集整理的关于增强型for循环的全部内容,更多相关增强型for循环内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部