增强型的for循环
来源:互联网 发布:caj阅读器 for mac 编辑:程序博客网 时间:2024/05/21 11:19
增强型的for循环 和普通for循环差不多
增强型的for循环 优点主要体现在集合中,随便举个例子
比如对 set 的遍历
一般是迭代遍历:Set<String> set = new HashSet<String>();Iterator<String> it = set.iterator(); while (it.hasNext()) { String str = it.next(); System.out.println(str); }for循环遍历: for (String str : set) { System.out.println(str); }
是不是简单些?
优点还体现在泛型 假如 set中存放的是Object
Set<Object> set = new HashSet<Object>();for循环遍历: for (Object obj: set) { if(obj instanceof Integer){ int aa= (Integer)obj; }else if(obj instanceof String){ String aa = (String)obj } ........ }
如果你用Iterator遍历,那就晕了
map list 也一样
唯一的缺点就是 在遍历 集合过程中,不能对集合本身进行操作
for (String str : set) { set.remove(str);//错误! }
0 0
- 增强型的for循环
- 增强的for循环
- 增强的for循环
- 增强型For循环
- 增强型for循环
- 增强型for循环
- 增强型for循环的各个参数
- 【for循环】增强的for循环
- java增强的for循环!
- 增强for循环的好处
- 增强的for循环语句
- Java增强的for循环
- 增强for循环的用法
- 增强for循环的优缺点
- Java5 增强的for循环
- 增强的for循环语句
- 增强for循环的使用
- 使用增强的for循环
- scala学习:环境搭建
- 单链表
- Squid缓存代理服务器安装过程
- Java:数列排序
- 高斯_约当消元(浮点)
- 增强型的for循环
- 默认 redis 安装存在漏洞, 可以直接 获取 root 权限
- 【2016.1.27】【数组】《求数组中的最大值和最小值》方法
- idea点击+号没有tomcat插件解决方法
- title属性中的内容包含大于号和小于号, 如何替换?
- [Leetcode]142. Linked List Cycle II @python
- java微信二次开发01
- TCP和UDP的异同点
- Java8大排序方法