http://m.blog.csdn.net/article/details?id=53743016
//String 数据的类型
//name 每次循环后将数据放入名为name的变量中
//names 被循环的数组或集合
foreach(String name : names){
syso(name);
}
for循环中的循环条件中的变量只求一次值!具体看最后的图片
•foreach语句是java5新增,在遍历数组、集合的时候,foreach拥有不错的性能。
•foreach是for语句的简化,但是foreach并不能替代for循环。可以这么说,任何foreach都能改写为for循环,但是反之则行不通。
•foreach不是java中的关键字。foreach的循环对象一般是一个集合,List、ArrayList、LinkedList、Vector、数组等。
for(元素类型T 每次循环元素的名称O : 循环对象){
//对O进行操作
}
一、常见使用方式。
1. foreach遍历数组。
1
2
3
4
5
6
7
8
9
10
11
12
|
/**
* 描述:
* Created by ascend on 2016/7/8.
*/
public class Client {
public static void main(String[] args) {
String[] names = { "beibei" , "jingjing" };
for (String name : names) {
System.out.println(name);
}
}
}
|
2.foreach遍历List。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/**
* 描述:
* Created by ascend on 2016/7/8.
*/
public class Client {
public static void main(String[] args) {
List<String> list = new ArrayList();
list.add( "a" );
list.add( "b" );
list.add( "c" );
for (String str : list){
System.out.println(str);
}
}
}
|
二、局限性。
foreach虽然能遍历数组或者集合,但是只能用来遍历,无法在遍历的过程中对数组或者集合进行修改,而for循环可以在遍历的过程中对源数组或者集合进行修改。
1.数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/**
* 描述:
* Created by ascend on 2016/7/8.
*/
public class Client {
public static void main(String[] args) {
String[] names = { "beibei" , "jingjing" };
for (String name : names) {
name = "huanhuan" ;
}
System.out.println( "foreach:" +Arrays.toString(names));
for ( int i = 0 ; i < names.length; i++) {
names[i] = "huanhuan" ;
}
System.out.println( "for:" +Arrays.toString(names));
}
}
输出:
foreach:[beibei, jingjing]
for :[huanhuan, huanhuan]
|
2.集合
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/**
* 描述:
* Created by ascend on 2016/7/8.
*/
public class Client {
public static void main(String[] args) {
List<String> names = new ArrayList<String>();
names.add( "beibei" );
names.add( "jingjing" );
for (String name:names){
name = "huanhuan" ;
}
System.out.println(Arrays.toString(names.toArray()));
for ( int i = 0 ; i < names.size(); i++) {
names.set(i, "huanhuan" );
}
System.out.println(Arrays.toString(names.toArray()));
}
}
输出:
[beibei, jingjing]
[huanhuan, huanhuan]
|
特别注意的地方!!
===================================================
===================================================
For-Each循环
For-Each循环也叫增强型的for循环,或者叫foreach循环。
For-Each循环是JDK5.0的新特性(其他新特性比如泛型、自动装箱等)。
For-Each循环的加入简化了集合的遍历。
其语法如下:
1
2
3
4
5
6
7
|
for (type
element: array)
{
System.out.println(element);
}
|
例子
代码中首先对比了两种for循环;之后实现了用增强for循环遍历二维数组;最后采用三种方式遍历了一个List集合。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
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 ( 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<String>
list = new
ArrayList<String>();
list.add( "a" );
list.add( "b" );
list.add( "c" );
System.out.println( "----------方式1-----------" );
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 (String
str: list)
{
System.out.println(str);
}
}
}
|
For-Each循环的缺点:丢掉了索引信息。
当遍历集合或数组时,如果需要访问集合或数组的下标,那么最好使用旧式的方式来实现循环或遍历,而不要使用增强的for循环,因为它丢失了下标信息。
以上就是小编为大家带来的浅谈java 增强型的for循环 for each的全部内容了
=========================================================================
========================================================================
5
为什么有些类可以可以用foreach遍历,有些类却不可以了.经反编译过后得出:
-------------------------------------------------------------------------------下面我们来看看自己如何实现一个类遍历-------------------------------------------------------------------------------
-
using
using
using
using
using
namespace
class
staticvoidstring
new
;
-
;
-
;
-
-
-
-
-
-
-
foreachstringin
-
-
-
-
-
-
publicclass
privatestringnewstring
publicint
get
returnthis
publicstringthisint
get
return
set
if
else
publicstring
get
set
publicint
get
set
publicstring
get
set
-
#region IEnumerable 成员
-
-
public
returnnew
-
#endregion
-
publicclass
publicstring
-
privatestring
-
privateint
-
#region IEnumerator 成员
-
publicobject
get
if
returnnull
return
-
publicbool
if
returnfalse
else
returntrue
publicvoid
-
#endregion
}