-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample04b.java
More file actions
57 lines (43 loc) · 1.34 KB
/
Example04b.java
File metadata and controls
57 lines (43 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
package com.study.collection;
import java.util.*;
public class Example04b {
static void printCollection(String s, Collection<String> c) {
String[] a = c.toArray(new String[0]);
Arrays.sort(a);
System.out.printf("%s: %s\n", s, Arrays.toString(a));
}
public static void main(String[] args) {
Collection<String> c1 = new Stack<>();
Collection<String> c2 = new LinkedList<>();
Collection<String> c3 = new ArrayList<>();
for (int i = 0; i < 20; i += 2) {
String s = String.format("%02d", i);
c1.add(s);
}
printCollection("c1 (2의 배수)", c1);
for (int i = 0; i < 20; i += 3) {
String s = String.format("%02d", i);
c2.add(s);
}
printCollection("c2 (3의 배수)", c2);
c3.clear();
for (String s : c1)
if (c2.contains(s)) c3.add(s);
printCollection("c1, c2 교집합", c3);
c3.clear();
for (String s : c1) c3.add(s);
for (String s : c1)
if (c2.contains(s)) c3.remove(s);
printCollection("c1, c2 차집합", c3);
c3.clear();
for (String s : c2) c3.add(s);
for (String s : c2)
if (c1.contains(s)) c3.remove(s);
printCollection("c2, c1 차집합", c3);
c3.clear();
for (String s : c1) c3.add(s);
for (String s : c2)
if (c3.contains(s) == false) c3.add(s);
printCollection("c1, c2 합집합", c3);
}
}