今天教大家用一種特殊的for 循環(huán). 是JDK 1.5 才出來的. 大家看看. 使用這樣的循環(huán)方便簡單 .. 呵呵 大家看看 例子吧.
1
public class StaticTest
2

{
3
public static void main (String[] args)
4
{
5
StaticTest dd=new StaticTest();
6
Employee[] staff=new Employee[3];
7
8
staff[0]= new Employee("xiaoqiao",60);
9
staff[1]= new Employee("haha",50);
10
staff[2]= new Employee("good",60);
11
for(Employee e:staff) //Employee 代表數(shù)據(jù)類型 e代表一個變量.就想當與普通for里的i變量一樣的 staff代表數(shù)組名
12
{
13
System.out.println("name="+e.getName()+" salary="+e.salary()); //然后調(diào)用2個方法 .每調(diào)用一次 e 從staff[0]一直到3
14
}
15
}
16
}
17
18
class Employee
19

{
20
private String name;
21
private double salary;
22
public Employee(String name,double salary)
23
{
24
this.name=name;
25
this.salary=salary;
26
}
27
28
public String getName()
29
{
30
return name;
31
}
32
public double salary()
33
{
34
return salary;
35
}
36
}

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

如果不使用這種特殊的for循環(huán) 那應(yīng)該是:
1
public class StaticTest
2

{
3
public static void main (String[] args)
4
{
5
StaticTest dd=new StaticTest();
6
Employee[] staff=new Employee[3];
7
8
staff[0]= new Employee("xiaoqiao",60);
9
staff[1]= new Employee("haha",50);
10
staff[2]= new Employee("good",60);
11
for(int i=0;i<3;i++)
12
{
13
System.out.println("name="+staff[i].getName()+" salary="+staff[i].salary());
14
}
15
}
16
}
17
18
class Employee
19

{
20
private String name;
21
private double salary;
22
public Employee(String name,double salary)
23
{
24
this.name=name;
25
this.salary=salary;
26
}
27
28
public String getName()
29
{
30
return name;
31
}
32
public double salary()
33
{
34
return salary;
35
}
36
}

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
