-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzengqiangFor.java
More file actions
58 lines (53 loc) · 1.33 KB
/
zengqiangFor.java
File metadata and controls
58 lines (53 loc) · 1.33 KB
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
package 排序;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class zengqiangFor {
/**
* 增强for循环的应用实例。
* 其中使用范围主要是:一维数组、二维数组和List
*/
public static void main(String[] args) {
//1、一维数组(普通数组)中的使用
int array[] = {1,2,3,4,5,6,7};
//增强for循环
for(int arritem : array){
System.out.println(arritem);
}
//普通的for循环
for(int i = 0; i < array.length; i++){
System.out.println(array[i]);
}
//2、二维数组中的使用
int array2[][] = {{1,2,3},{4,5,6},{7,8,9}};
//增强的for循环
for(int arrayitem[] : array2){
for(int item : arrayitem){
System.out.println(item);
}
}
//普遍的二维数组的for循环
for(int i = 0; i < array2.length; i++){
for(int j = 0; j < array2[i].length; j++){
System.out.println(array2[i][j]);
}
}
//在List中的使用
List<String> list = new ArrayList<String>();
list.add("xxx");
list.add("yyy");
list.add("zzz");
//增强for的使用
for(String item : list){
System.out.println(item);
}
//一般情况下的for循环
for(int i = 0; i < list.size(); i++){
System.out.println(list.get(i));
}
//迭代器遍历
for(Iterator<String> iterator = list.iterator(); iterator.hasNext();){
System.out.println(iterator.next());
}
}
}