-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForBase.java
More file actions
71 lines (57 loc) · 1.62 KB
/
ForBase.java
File metadata and controls
71 lines (57 loc) · 1.62 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
59
60
61
62
63
64
65
66
67
68
69
70
71
package example.ForEachExample;
import java.util.ArrayList;
import java.util.List;
/**
* for loop
*
* keyword:
* - for loop
* - a++, ++a
*
* Reference:
* - https://ithelp.ithome.com.tw/articles/10283750
*/
public class ForBase {
public static void main(String[] args) {
baseForLoop();
forEachExample();
aPlusPlusOrPlusPlusA();
}
public static List<String> generateData() {
List<String> students = new ArrayList<>();
students.add("george");
students.add("peter");
students.add("may");
students.add("john");
students.add("JJ");
students.add("GG");
return students;
}
public static void baseForLoop() {
// * basic for loop.
List<String> students = generateData();
// list 裡面是放 string
for (int i=0; i<students.size(); i++) {
System.out.println(students.get(i));
}
// list 裡面是放 object
for (int i=0; i<students.size(); i++) {
// TODO: build a example to demo object.
}
}
public static void forEachExample() {
// using foreach.
List<String> students = generateData();
students.forEach(s -> {
System.out.println(s);
});
}
public static void aPlusPlusOrPlusPlusA () {
// * 區隔 a++ 和 ++a 差別
Integer numOne = 1;
Integer numTwo = 5;
System.out.println(numOne++); // 1
System.out.println(++numTwo); // 6
}
// TODO: add another examples.
}