Last active
June 7, 2019 09:54
-
-
Save mdstoy/ae072f35aece925fd6031784086f18cf to your computer and use it in GitHub Desktop.
「もう参照渡しとは言わせない 2018 冬」用 サンプルコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class R { | |
public static void main(String[] args) { | |
List<String> x = new ArrayList<>(); | |
System.out.printf("x: %s\n", x); // "x: []" | |
add(x); | |
System.out.printf("x: %s\n", x); // "x: ["xxx"]" | |
} | |
public static void add(List<String> a) { | |
a.add("xxx"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pseudocode | |
swap(int a, int b) { | |
int tmp = a | |
a = b | |
b = tmp | |
} | |
x = 1 | |
y = 2 | |
swap(x, y) | |
// x = 2, y = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Reference { | |
public static void main(String[] args) { | |
List<String> x = List.of("aaa"); | |
List<String> y = List.of("bbb"); | |
System.out.printf("x: %s, y: %s\n", x, y); // "x: ["aaa"], y: ["bbb"]" | |
swap(x, y); | |
System.out.printf("x: %s, y: %s\n", x, y); // "x: ["aaa"], y: ["bbb"]" | |
} | |
public static void swap(List<String> a, List<String> b) { | |
List<String> tmp = a; | |
a = b; | |
b = tmp; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pseudocode | |
swap(int a, int b) { | |
int tmp = a | |
a = b | |
b = tmp | |
} | |
x = 1 | |
y = 2 | |
swap(x, y) | |
// x = 1, y = 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Value { | |
public static void main(String[] args) { | |
int x = 1; | |
int y = 2; | |
System.out.printf("x: %d, y: %d\n", x, y); // "x: 1, y: 2" | |
swap(x, y); | |
System.out.printf("x: %d, y: %d\n", x, y); // "x: 1, y: 2" | |
} | |
public static void swap(int a, int b) { | |
int tmp = a; | |
a = b; | |
b = tmp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment