Skip to content

Commit 1c855f9

Browse files
committed
example
1 parent 69eb62b commit 1c855f9

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

Containers/example.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.*;
2+
3+
class Unsupported {
4+
static void test(String msg, List<String> list) {
5+
System.out.println("--- " + msg + " ---");
6+
Collection<String> c = list;
7+
Collection<String> subList = list.subList(1, 8);
8+
// Copy of the sublist:
9+
Collection<String> c2 = new ArrayList<String>(subList);
10+
try {
11+
c.retainAll(c2);
12+
} catch (Exception e) {
13+
System.out.println("retainAll(): " + e);
14+
}
15+
try {
16+
c.removeAll(c2);
17+
} catch (Exception e) {
18+
System.out.println("removeAll(): " + e);
19+
}
20+
try {
21+
c.clear();
22+
} catch (Exception e) {
23+
System.out.println("clear(): " + e);
24+
}
25+
try {
26+
c.add("X");
27+
} catch (Exception e) {
28+
System.out.println("add(): " + e);
29+
}
30+
try {
31+
c.addAll(c2);
32+
} catch (Exception e) {
33+
System.out.println("addAll(): " + e);
34+
}
35+
try {
36+
c.remove("C");
37+
} catch (Exception e) {
38+
System.out.println("remove(): " + e);
39+
}
40+
// The List.set() method modifies the value but
41+
// doesn’t change the size of the data structure:
42+
try {
43+
list.set(0, "X");
44+
} catch (Exception e) {
45+
System.out.println("List.set(): " + e);
46+
}
47+
}
48+
49+
public static void main(String[] args) {
50+
List<String> list = Arrays.asList("A B C D E F G H I J K L".split(" "));
51+
test("Modifiable Copy", new ArrayList<String>(list));
52+
test("Arrays.asList()", list);
53+
test("unmodifiableList()",
54+
Collections.unmodifiableList(
55+
new ArrayList<String>(list)));
56+
}
57+
}

0 commit comments

Comments
 (0)