-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwapExample.java
More file actions
58 lines (50 loc) · 1.34 KB
/
SwapExample.java
File metadata and controls
58 lines (50 loc) · 1.34 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 example.MethodExample;
/**
* Swap觀念
*
* Reference:
* - https://java.4-x.tw/java-07/java-swap
*
*/
public class SwapExample {
public static void main(String[] args) {
swap(2, 3);
Data data = new Data();
data.x = 11;
data.y = 22;
swap(data);
}
/**
* Call by value example.
*
* @param a
* @param b
*/
private static void swap(Integer a, Integer b) {
// ! main裡面的變數a和b,並不是swap方法裡面的a和b。也就是說swap方法的a和b只是main方法裡面a和b的複製品。
// TODO: 是否有抓取變數的位址?
System.out.println("before result:");
System.out.println("a: " + a + "; b: " + b);
Integer temp;
temp = a;
a = b;
b = temp;
System.out.println("after result:");
System.out.println("a: " + a + "; b: " + b);
}
/**
* Call by reference example.
*
* @param Data
*/
public static void swap(Data data) {
System.out.println("before result:");
System.out.println("x: " + data.x+ "; y: " + data.y);
Integer temp;
temp = data.x;
data.x = data.y;
data.y = temp;
System.out.println("after result:");
System.out.println("x: " + data.x + "; y: " + data.y);
}
}