Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Using for and foreach loops

Java Examples - Using for and foreach Loops

Java Examples Java Examples

The for statement is fairly simple and is used to loop over data.

The number of times a for loop executes is determined before execution. The syntax is as follows:


for(初始化; 布尔表达式; 更新) {
    //code statement
}

The foreach statement is one of the new features of Java 5; when iterating over arrays and collections, foreach provides great convenience for developers.

The foreach syntax is as follows:


for(元素类型t 元素变量x : 遍历对象obj){ 
     引用了x的java语句; 
} 

The following example demonstrates the use of for and foreach loops:

Main.java File

public

class

Main

{

public

static

void

main

(

String

[

]

args

)

{

int

[

]

intary

=

{

1

,

2

,

3

,

4

}

;

forDisplay

(

intary

)

;

foreachDisplay

(

intary

)

;

}

public

static

void

forDisplay

(

int

[

]

a

)

{

System

.

out

.

println

(

"

Looping over an array with for

"

)

;

for

(

int

i

=

0

;

i

<

a

.

length

;

i

++

)

{

System

.

out

.

print

(

a

[

i

]

"

"

)

;

}

System

.

out

.

println

(

)

;

}

public

static

void

foreachDisplay

(

int

[

]

data

)

{

System

.

out

.

println

(

"

Looping over an array with foreach

"

)

;

for

(

int

a

:

data

)

{

System

.

out

.

print

(

a

"

"

)

;

}

}

}

The output of the above code is:


使用 for 循环数组
1 2 3 4 
使用 foreach 循环数组
1 2 3 4

Main.java File

import

java

.

util

.

ArrayList

;

import

java

.

util

.

Iterator

;

import

java

.

util

.

List

;

public

class

Main

{

public

static

void

main

(

String

[

]

args

)

{

int

[

]

arr

=

{

1

,

2

,

3

,

4

,

5

}

;

System

.

out

.

println

(

"

----------Using a for loop------------

"

)

;

for

(

int

i

=

0

;

i

<

arr

.

length

;

i

++

)

{

System

.

out

.

println

(

arr

[

i

]

)

;

}

System

.

out

.

println

(

"

---------Using a For-Each loop-------------

"

)

;

//

Enhanced for loop For-Each

for

(

int

element

:

arr

)

{

System

.

out

.

println

(

element

)

;

}

System

.

out

.

println

(

"

---------For-Each loop over a two-dimensional array-------------

"

)

;

//

Iterating over a two-dimensional array

int

[

]

[

]

arr2

=

{

{

1

,

2

,

3

}

,

{

4

,

5

,

6

}

,

{

7

,

8

,

9

}

}

;

for

(

int

[

]

row

:

arr2

)

{

for

(

int

element

:

row

)

{

System

.

out

.

println

(

element

)

;

}

}

//

Iterating over a List collection in three ways

List

<
String
list
=
new
ArrayList
<
String
(
)

;

list

.

add

(

"

Google

"

)

;

list

.

add

(

"

Runoob

"

)

;

list

.

add

(

"

Taobao

"

)

;

System

.

out

.

println

(

"

----------Way 1: ordinary for loop-----------

"

)

;

for

(

int

i

=

0

;

i

<

list

.

size

(

)

;

i

++

)

{

System

.

out

.

println

(

list

.

get

(

i

)

)

;

}

System

.

out

.

println

(

"

----------Way 2: using an iterator-----------

"

)

;

for

(

Iterator

<
String
iter
=
list

.

iterator

(

)

;

iter

.

hasNext

(

)

;

)

{

System

.

out

.

println

(

iter

.

next

(

)

)

;

}

System

.

out

.

println

(

"

----------Way 3: For-Each loop-----------

"

)

;

for

(

String

str

:

list

)

{

System

.

out

.

println

(

str

)

;

}

}

}

The output of the above code is:


----------Using for loop------------
1
2
3
4
5
---------Using For-Each Loop-------------
1
2
3
4
5
---------For-Each Loop for 2D Array-------------
1
2
3
4
5
6
7
8
9
----------Method 1: Standard for loop-----------
Google
Runoob
Taobao
----------Method 2: Using an iterator-----------
Google
Runoob
Taobao
----------Method 3: For-Each Loop-----------
Google
Runoob
Taobao

Java Examples Java Examples