public class FixedLists
1: import java.util.List;
2: import java.util.ArrayList;
3: import java.util.ListIterator;
4: import java.util.Arrays;
5: import java.util.Collections;
6: import java.util.Scanner;
8: /**
9: * Demonstrate some of the ways of making unmodifiable lists.
10: *
11: * @author Mark Young
12: */
13: public class FixedLists {
14:
15: public static void main(String[] args) {
16: // create variables
17: List<Integer> nums;
18: List<String> words;
19: ListIterator<String> it;
20:
21: // show that normal lists can be modified
22: System.out.println("Normal Lists are fully modifiable");
23: words = new ArrayList<>();
24: words.add("words");
25: words.add("here");
26: words.add("too");
27: System.out.println("Normal List: " + words);
28: it = words.listIterator();
29: try {
30: String next = it.next();
31: System.out.println("--> " + next);
32: it.add("NEW");
33: System.out.println("added NEW");
34: } catch (UnsupportedOperationException uoe) {
35: System.out.println("add FAILED: " + uoe);
36: }
37: try {
38: String next = it.next();
39: System.out.println("--> " + next);
40: it.set(next.toUpperCase());
41: System.out.println("set to uppercase");
42: } catch (UnsupportedOperationException uoe) {
43: System.out.println("set FAILED: " + uoe);
44: }
45: try {
46: String next = it.next();
47: System.out.println("--> " + next);
48: it.remove();
49: System.out.println("removed");
50: } catch (UnsupportedOperationException uoe) {
51: System.out.println("remove FAILED: " + uoe);
52: }
53: System.out.println("Normal List: " + words);
54: pause();
56: // create and show a couple of immutable lists
57: System.out.println("But sometimes we want lists that can't be changed."
58: + "\nThere are several ways of doing that.");
59: pause();
60:
61: // create some unmodificable lists using List.of
62: nums = List.of(1, 2, 3, 4, 5);
63: words = List.of("some", "words", "here");
64: System.out.println("Immutable List created using List.of: " + nums);
65: System.out.println("Immutable List created using List.of: " + words);
66: pause();
68: // show that no changes are allowed in List.of lists
69: System.out.println("No changes allowed to immutable lists");
70: it = words.listIterator();
71: try {
72: String next = it.next();
73: System.out.println("--> " + next);
74: it.add("NEW");
75: System.out.println("added NEW");
76: } catch (UnsupportedOperationException uoe) {
77: System.out.println("add FAILED: " + uoe);
78: }
79: try {
80: String next = it.next();
81: System.out.println("--> " + next);
82: it.set(next.toUpperCase());
83: System.out.println("set to uppercase");
84: } catch (UnsupportedOperationException uoe) {
85: System.out.println("set FAILED: " + uoe);
86: }
87: try {
88: String next = it.next();
89: System.out.println("--> " + next);
90: it.remove();
91: System.out.println("removed");
92: } catch (UnsupportedOperationException uoe) {
93: System.out.println("remove FAILED: " + uoe);
94: }
95: System.out.println("Immutable List created using List.of: " + words);
96: pause();
98: // show no changes allowed in Collections.unmodifiableList lists
99: System.out.println("No changes allowed in unmodifiable Lists, either.");
100: words = new ArrayList<>();
101: words.add("words");
102: words.add("here");
103: words.add("too");
104: words = Collections.unmodifiableList(words);
105: System.out.println("Unmodifiable List created using "
106: + "Collections.unmodifiableList: " + words);
107: it = words.listIterator();
108: try {
109: String next = it.next();
110: System.out.println("--> " + next);
111: it.add("NEW");
112: System.out.println("added NEW");
113: } catch (UnsupportedOperationException uoe) {
114: System.out.println("add FAILED: " + uoe);
115: }
116: try {
117: String next = it.next();
118: System.out.println("--> " + next);
119: it.set(next.toUpperCase());
120: System.out.println("set to uppercase");
121: } catch (UnsupportedOperationException uoe) {
122: System.out.println("set FAILED: " + uoe);
123: }
124: try {
125: String next = it.next();
126: System.out.println("--> " + next);
127: it.remove();
128: System.out.println("removed");
129: } catch (UnsupportedOperationException uoe) {
130: System.out.println("remove FAILED: " + uoe);
131: }
132: System.out.println("Unmodifiable List: " + words);
133: pause();
134:
135: // introduce Arrays.asList
136: System.out.println("Arrays.asList just wraps around an array.\n"
137: + "You can modify list elements, but not add/remove them.");
138: pause();
139:
140: // show that Arrays.asList allows no adds/removes
141: String[] array = {"more", "words", "here"};
142: words = Arrays.asList(array);
143: System.out.println("Unmodifiable List created using "
144: + "Arrays.asList: " + words);
145: System.out.println(" -> the array: " + Arrays.toString(array));
146: System.out.println(" -> the List: " + words);
147: it = words.listIterator();
148: try {
149: String next = it.next();
150: System.out.println("--> " + next);
151: it.add("NEW");
152: System.out.println("added NEW");
153: } catch (UnsupportedOperationException uoe) {
154: System.out.println("add FAILED: " + uoe);
155: }
156: try {
157: String next = it.next();
158: System.out.println("--> " + next);
159: it.set(next.toUpperCase());
160: System.out.println("set to uppercase");
161: } catch (UnsupportedOperationException uoe) {
162: System.out.println("set FAILED: " + uoe);
163: }
164: try {
165: String next = it.next();
166: System.out.println("--> " + next);
167: it.remove();
168: System.out.println("removed");
169: } catch (UnsupportedOperationException uoe) {
170: System.out.println("remove FAILED: " + uoe);
171: }
172: System.out.println(" -> the array: " + Arrays.toString(array));
173: System.out.println(" -> the List: " + words);
174: pause();
175:
176: // show that the array and list share memory
177: System.out.println("You can modify the Arrays.asList "
178: + "by changing the array:");
179: System.out.println("--> array[0] = \"different\"");
180: array[0] = "different";
181: System.out.println(" -> the array: " + Arrays.toString(array));
182: System.out.println(" -> the List: " + words);
183: pause();
184: }
186: private static final Scanner kbd = new Scanner(System.in);
187: private static void pause() {
188: System.out.println();
189: System.out.print("...press enter...");
190: kbd.nextLine();
191: System.out.println();
192: }
194: }