Source of ListManipulations.java


  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:         printListCounted(myWords);
 53:         pause();

 55:         // print the List in various ways
 56:         printListForEach(myWords);
 57:         pause();
 58:         printListWithIterator(myWords);
 59:         pause();
 60:     }

 62:     /**
 63:      * Print the elements of a List of String in four columns 15 characters
 64:      * wide.
 65:      *
 66:      * @param theList the list to print
 67:      */
 68:     public static void printListCounted(List<String> theList) {
 69:         System.out.println("Printing the list using a count-control loop.");
 70:         for (int i = 0; i < theList.size(); ++i) {
 71:             if (i % 4 == 0) {
 72:                 System.out.println();
 73:             }
 74:             System.out.printf("%15s", theList.get(i));
 75:         }
 76:         System.out.println();
 77:     }

 79:     /**
 80:      * Remove all words starting with 'f' from the given list.
 81:      *
 82:      * @param theList the list to remove f-words from
 83:      */
 84:     public static void removeFWords(List<String> theList) {
 85:         ListIterator<String> it = theList.listIterator();
 86:         while (it.hasNext()) {
 87:             String x = it.next();
 88:             if (x.startsWith("f")) {
 89:                 it.remove();
 90:             }
 91:         }
 92:     }

 94:     /**
 95:      * Add a warning before all words starting with 'f' in the given list.
 96:      *
 97:      * @param theList the list to remove f-words from
 98:      */
 99:     public static void addFWords(List<String> theList) {
100:         ListIterator<String> it = theList.listIterator();
101:         while (it.hasNext()) {
102:             String x = it.next();
103:             if (x.startsWith("f")) {
104:                 it.add("f-WORD_ALERT!");
105:             }
106:         }
107:     }

109:     /**
110:      * Beep out all words starting with 'f' in the given list.
111:      *
112:      * @param theList the list to remove f-words from
113:      */
114:     public static void beepFWords(List<String> theList) {
115:         ListIterator<String> it = theList.listIterator();
116:         while (it.hasNext()) {
117:             String x = it.next();
118:             if (x.startsWith("f")) {
119:                 it.set("BEEEEEP!");
120:             }

122:         }

124:     }

126:     /**
127:      * Print the elements of a List of String in four columns 15 characters
128:      * wide.
129:      *
130:      * @param theList the list to print
131:      */
132:     public static void printListForEach(List<String> theList) {
133:         System.out.println("Printing the list using a for-each loop.");
134:         // TO DO
135:         System.out.println("... for you to do! ...");
136:     }

138:     /**
139:      * Print the elements of a List of String in four columns 15 characters
140:      * wide.
141:      *
142:      * @param theList the list to print
143:      */
144:     public static void printListWithIterator(List<String> theList) {
145:         System.out.println("Printing the list using a list iterator.");
146:         ListIterator<String> it = theList.listIterator();
147:         final int ON_EACH_LINE = 4;
148:         int soFarThisLine = 0;
149:         while (it.hasNext()) {
150:             if (soFarThisLine == ON_EACH_LINE) {
151:                 System.out.println();
152:                 soFarThisLine = 0;
153:             }
154:             String x = it.next();
155:             System.out.printf("%15s", x);
156:             ++soFarThisLine;
157:         }
158:         System.out.println();
159:     }

161:     private static final Scanner kbd = new Scanner(System.in);

163:     /**
164:      * Waits for the user to press pause (after prompting them to do so).
165:      */
166:     private static void pause() {
167:         System.out.println();
168:         System.out.print("...press enter...");
169:         kbd.nextLine();
170:         System.out.println();
171:     }

173: }