Java foreach
foreach循环也叫增强型的for循环,或者叫foreach循环。
foreach循环是JDK5.0的新特性(其他新特性比如泛型、自动装箱等)。
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int arr[] = new int[4];
for (int x : arr) {
System.out.println(x);
}
for (int i = 3; i > 0; i--) {
arr[i] = i;
}
System.out.println(Arrays.toString(arr));
}
}
0
0
0
0
[0, 1, 2, 3]
对它进行修改:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int arr[] = new int[4];
for(int num:arr){
num=3;
}
System.out.println(Arrays.toString(arr));
}
}
[0, 0, 0, 0]
发现修改对其无效,但是也不报错。
add:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class Main {
public static void main(String[] args) {
String[] strArray={"111","222","333","444"};
List<String> list=new ArrayList<String>(Arrays.asList(strArray));
System.out.println(list);
for(String str:list){
System.out.println(str);
list.add("sss");
System.out.println("添加后:"+list);
}
}
}
Exception in thread "main" java.util.ConcurrentModificationException
[111, 222, 333, 444]
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
111
at java.util.ArrayList$Itr.next(ArrayList.java:851)
添加后:[111, 222, 333, 444, sss]
at com.qhong.Main.main(Main.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
remove:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class Main {
public static void main(String[] args) {
String[] strArray={"111","222","333","444"};
List<String> list=new ArrayList<String>(Arrays.asList(strArray));
System.out.println(list);
for(String str:list){
System.out.println(str);
list.remove(str);
System.out.println("移除后:"+list);
}
}
}
[111, 222, 333, 444]
Exception in thread "main" java.util.ConcurrentModificationException
111
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
移除后:[222, 333, 444]
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at com.qhong.Main.main(Main.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) Process finished with exit code 1
foreach这种循环一般只适合做数组的遍历,提取数据显示等,不适合用于添加删除和使用下标等复杂的操作。
foreach语句是for语句特殊情况下的增强版本,简化了编程,提高了代码的可读性和安全性(不用怕数组越界)。相对老的for语句来说是个很好的补充。
foreach循环的缺点:丢掉了索引信息。
当遍历集合或数组时,如果需要访问集合或数组的下标,那么最好使用旧式的方式来实现循环或遍历,而不要使用增强的for循环,因为它丢失了下标信息。
Java foreach的更多相关文章
- java foreach 循环原理
java foreach 语法是在jdk1.5时加入的新特性,主要是当作for语法的一个增强,那么它的底层到底是怎么实现的呢?因为面试时被问到,所以在这边做一个记录. 首先来看看foreach能够使用 ...
- Java for-each循环解惑
Java for-each循环解惑 2014/04/24 | 分类: 技术之外 | 0 条评论 | 标签: JAVA 分享到:21 本文由 ImportNew - liqing 翻译自 javarev ...
- java foreach编辑讲解
foreach语句使用总结 foreach语句是java5的新特征之一,在遍历数组.集合方面,foreach为开发人员提供了极大的方便. foreach语句是for语句的特殊简化版本,但是foreac ...
- Java foreach操作(遍历)数组
语法: 我们分别使用 for 和 foreach 语句来遍历数组 运行结果: 练习: import java.util.Arrays; public class HelloWorld { public ...
- java foreach实现原理
在平时Java程序中,应用比较多的就是对Collection集合类的foreach遍历,foreach之所以能工作,是因为这些集合类都实现了Iterable接口,该接口中定义了Iterator迭代器的 ...
- 反编译看java for-each循环
java 1.5发行版引入的for-each循环.(引自<Effective Java>中文版第二版 第46条) 如以下对数组列表的for-each循环示例: public class F ...
- java Foreach与迭代器
foreach语法主要用于数组,但是它也可以用于Collection对象,下面是一个示例 package object; //: holding/ForEachCollections.java // ...
- Java foreach remove问题分析
原文链接:http://www.cnblogs.com/chrischennx/p/9610853.html 都说ArrayList在用foreach循环的时候,不能add元素,也不能remove元素 ...
- Java Foreach用法
java中的while.for.if.switch等用法和c语言差不多,所以我们关注下foreach就行了. 一.创建ForeachTest.java public class ForeachTest ...
随机推荐
- Scrum会议8(Beta版本)
组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...
- PHP file_get_contents函数读取远程数据超时的解决方法
PHP file_get_contents函数读取远程数据超时的解决方法 投稿:junjie 字体:[增加 减小] 类型:转载 这篇文章主要介绍了PHP file_get_contents函数读取 ...
- Bootstrap页面布局20 - BS缩略图
<div class='container-fluid'> <h2 class='page-header'>Bootstrap 缩略图</h2> <ul cl ...
- jquery循环绑定事件
<html> <head> <title></title> <script type="text/javascript" sr ...
- 6月辞职->帝都生活
---恢复内容开始--- 5月初送走了静,有点伤心,但还是忍住没哭. 纠结了一下上哪个班,上不上基础班,不能再拖了,果断交钱报6月份的ios基础班.之前还有个电话面试,怕怕的,考了很多函数的知识,好多 ...
- 拼写纠正 Artificial Intelligence: A Modern Approach
Artificial Intelligence: A Modern Approach http://mindhacks.cn/2008/09/21/the-magical-bayesian-metho ...
- Java笔试面试题二(常考问答)转
1.说出ArrayList,Vector, LinkedList的存储性能和特性 ArrayList 和Vector都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允 ...
- pro9
1.本次课学习到的知识点 C语言的几个基本数据类型 各种基本数据类型的常量的表现形式 C语言的表达式个中表达式的求解规则 2.实验过程中遇到的问题及解决方法: 不太理解完数的概念以及如何判断完数,另外 ...
- docker nexus oss
docker login/search x.x.x.x:8081 sonatype/docker-nexus Docker images for Sonatype Nexus with the Ora ...
- Qt工具知多少(一目了然)
一级题目: Qt Designer — 所见即所得的界面设计工具, 可以用拖拽的方式将控件排布在界面上,支持layout, 支持signal/slot编辑. 生成的文件保存为ui格式, ui是xml格 ...