For-Each循环
For-Each循环也叫增强型的for循环,或者叫foreach循环。
For-Each循环是JDK5.0的新特性(其他新特性比如泛型、自动装箱等)。
For-Each循环的加入简化了集合的遍历。
其语法如下:
for(type element: array)
{
System.out.println(element);
}
例子
其基本使用可以直接看代码:
代码中首先对比了两种for循环;之后实现了用增强for循环遍历二维数组;最后采用三种方式遍历了一个List集合。
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ForeachTest
{
public static void main(String[] args)
{
int[] arr = {1, 2, 3, 4, 5};
System.out.println("----------旧方式遍历------------");
//旧式方式
for(int i=0; i<arr.length; i++)
{
System.out.println(arr[i]);
}
System.out.println("---------新方式遍历-------------");
//新式写法,增强的for循环
for(int element:arr)
{
System.out.println(element);
}
System.out.println("---------遍历二维数组-------------");
//遍历二维数组
int[][] arr2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} ;
for(int[] row : arr2)
{
for(int element : row)
{
System.out.println(element);
}
}
//以三种方式遍历集合List
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
System.out.println("----------方式1-----------");
//第一种方式,普通for循环
for(int i = 0; i < list.size(); i++)
{
System.out.println(list.get(i));
}
System.out.println("----------方式2-----------");
//第二种方式,使用迭代器
for(Iterator<String> iter = list.iterator(); iter.hasNext();)
{
System.out.println(iter.next());
}
System.out.println("----------方式3-----------");
//第三种方式,使用增强型的for循环
for(String str: list)
{
System.out.println(str);
}
}
}
For-Each循环的缺点:丢掉了索引信息。
当遍历集合或数组时,如果需要访问集合或数组的下标,那么最好使用旧式的方式来实现循环或遍历,而不要使用增强的for循环,因为它丢失了下标信息。
转载自:http://www.cnblogs.com/mengdd/archive/2013/01/21/2870019.html
参考:http://www.jb51.net/article/35884.htm
http://www.importnew.com/11038.html
分享到:
相关推荐
下面小编就为大家带来一篇浅谈java 增强型的for循环 for each。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
VB For each循环实例 VB For each循环实例
for_each 返回值实例详解;
MATLAB工具箱大全-将多重循环转为单重循环-For-Each
下面是用一个传统的for循环遍历集合的例子:用传统的for循环遍历数组的做法如下:这些做法逗比while循环更好,但是它们并不完美。如果真的那么不幸,并且外部集
本文详解了JAVA中的for-each循环与迭代,是JS入门学习中的基础知识,也是java中的难点知识,需要的朋友可以参考下 。
从Java5起,在Java中有了for-each循环,可以用来循环遍历collection和array。For each循环允许你在无需保持传统for循环中的索引,或在使用iterator /ListIterator时无需调用while循环中的hasNext()方法就能遍历...
值得注意的是,Python的for循环不同于其他语言中的传统for循环,它更像是“for each”循环,直接遍历可迭代对象的元素,而不需要手动管理索引或迭代器。这种简洁的语法使得for循环在Python中非常常用且易于理解。 ...
For 和 Foreach 的效率问题,先猜一下,再运行一下,看猜的对不对。
By highlighting the features and comparative elements of each language, and building on the reader's existing knowledge of object-oriented programming, C++ for Java Programmers enables users to ...
其实查看jQuery的源代码,发现each的代码很简单,但为什么性能和原生的for循环相差几十倍呢? jQuery的each的核心代码 for (; i < length; i++) { value = callback.call(obj[i], i, obj[i]); if (value === ...
Write a Java program called ContinueLoop.java that uses a for loop with the variable "count" and count 1 to 10.. Display "count=<count>" each time you loop through. Skip the display statement using ...
Learn Java for Android Development, Third Edition, is an update of a strong selling book that now includes a primer on Android app development (in Chapter 1 and Appendix C, which is distributed in the...
Author Michael Brzustowicz explains the basic math theory behind each step of the data science process, as well as how to apply these concepts with Java. You’ll learn the critical roles that data ...
本文实例分析了js的for in循环和java里foreach循环的区别。分享给大家供大家参考。具体分析如下: js里的for in循环定义如下: 代码如下:for(var variable in obj) { … } obj可以是一个普通的js对象或者一个数组。...
java1.7-part1
Write a Java program called BreakLoop.java that uses a for loop with the variable "count" and count 1 to 10.. Display "count=<count>" each time you loop through. Break out of the loop at 5. At the end...
查些资料,并且试验过发现下面方法可以 1、for循环中我们使用continue;终止本次循环计入下一个循环,使用break终止整个循环。 2、而在jquery中 $.each则对应的使用return true 和return false。 break可以跳出switch...
Create two suclasses for individual automobile makers (for example, Ford and Chevy) and include appropriate setPrice() methods in each subclass. Finally, write a program that uses the Auto class and ...
for_each遍历vector vector中可以是基本类型(int,double,char,string) 也可以是类类型