public class ListManipulations
2: import java.util.List;
3: import java.util.LinkedList;
4: import java.util.ListIterator;
6: import java.util.Collections;
7: import java.util.Scanner;
9: /**
10: * Use various loops on lists.
11: *
12: * @author s9980739
13: */
14: public class ListManipulations {
16: /**
17: * @param args the command line arguments
18: */
19: public static void main(String[] args) {
20: List<String> myWords = new LinkedList<>();
22: // add some values to it (on the end)
23: myWords.add("ten");
24: myWords.add("twenty");
25: myWords.add("Thirty");
26: myWords.add("forty");
27: myWords.add("Fifty");
28: myWords.add("twenty");
29: System.out.println("Here is a list: " + myWords);
30: pause();
32: // add some values (not on the end)
33: System.out.println("Adding 15 @ location 1, then 45 @ location 5.");
34: myWords.add(1, "fifteen");
35: myWords.add(5, "forty-five");
36: System.out.println("Here is the list: " + myWords);
37: pause();
39: // sort the list
40: System.out.println("Sorting the list.");
41: Collections.sort(myWords, String.CASE_INSENSITIVE_ORDER);
42: System.out.println("Here is the list: " + myWords);
43: pause();
45: // print and manipulate the list in multiple ways
46: printListCounted(myWords);
47: pause();
49: // manipulation
50: System.out.println("Beeping out the F-words.");
51: beepFWords(myWords);
52: // capitalizeFWords(myWords);
53: // duplicateFWords(myWords);
54: printListCounted(myWords);
55: pause();
57: // print the List in various ways
58: printListForEach(myWords);
59: pause();
60: printListWithIterator(myWords);
61: pause();
62: }
64: /**
65: * Print the elements of a List of String in four columns 15 characters
66: * wide.
67: *
68: * @param theList the list to print
69: */
70: public static void printListCounted(List<String> theList) {
71: System.out.println("Printing the list using a count-control loop.");
72: for (int i = 0; i < theList.size(); ++i) {
73: if (i % 4 == 0) {
74: System.out.println();
75: }
76: System.out.printf("%15s", theList.get(i));
77: }
78: System.out.println();
79: }
81: /**
82: * Remove all words starting with 'f' from the given list.
83: *
84: * @param theList the list to remove f-words from
85: */
86: public static void removeFWords(List<String> theList) {
87: ListIterator<String> it = theList.listIterator();
88: while (it.hasNext()) {
89: String x = it.next();
90: if (x.startsWith("f")) {
91: it.remove();
92: }
93: }
94: }
96: /**
97: * Add a warning before all words starting with 'f' in the given list.
98: *
99: * @param theList the list to remove f-words from
100: */
101: public static void addFWords(List<String> theList) {
102: ListIterator<String> it = theList.listIterator();
103: while (it.hasNext()) {
104: String x = it.next();
105: if (x.startsWith("f")) {
106: it.add("f-WORD_ALERT!");
107: }
108: }
109: }
111: /**
112: * Beep out all words starting with 'f' in the given list.
113: *
114: * @param theList the list to remove f-words from
115: */
116: public static void beepFWords(List<String> theList) {
117: ListIterator<String> it = theList.listIterator();
118: while (it.hasNext()) {
119: String x = it.next();
120: if (x.startsWith("f")) {
121: it.set("BEEEEEP!");
122: }
124: }
126: }
128: /**
129: * Print the elements of a List of String in four columns 15 characters
130: * wide.
131: *
132: * @param theList the list to print
133: */
134: public static void printListForEach(List<String> theList) {
135: System.out.println("Printing the list using a for-each loop.");
136: // TO DO
137: System.out.println("... for you to do! ...");
138: }
140: /**
141: * Print the elements of a List of String in four columns 15 characters
142: * wide.
143: *
144: * @param theList the list to print
145: */
146: public static void printListWithIterator(List<String> theList) {
147: System.out.println("Printing the list using a list iterator.");
148: ListIterator<String> it = theList.listIterator();
149: final int ON_EACH_LINE = 4;
150: int soFarThisLine = 0;
151: while (it.hasNext()) {
152: if (soFarThisLine % ON_EACH_LINE == 0) {
153: soFarThisLine = 0;
154: System.out.println();
155: }
156: String x = it.next();
157: System.out.printf("%15s", x);
158: ++soFarThisLine;
159: }
160: System.out.println();
161: }
163: private static final Scanner kbd = new Scanner(System.in);
165: /**
166: * Waits for the user to press pause (after prompting them to do so).
167: */
168: private static void pause() {
169: System.out.println();
170: System.out.print("...press enter...");
171: kbd.nextLine();
172: System.out.println();
173: }
175: }