Source of ListManipulations.java


  1: import java.util.LinkedList;
  2: import java.util.List;
  3: import java.util.ListIterator;
  4: import java.util.Collections;
  5: 
  6: /**
  7:  * A program to demonstrate some list manipulations.
  8:  *
  9:  * @author Mark Young (A00000000)
 10:  */
 11: public class ListManipulations {
 12: 
 13:     /**
 14:      * Do it all.
 15:      *
 16:      * @param args the command line arguments (ignored)
 17:      */
 18:     public static void main(String[] args) {
 19:         // create a List with some words
 20:         List<String> myWords = new LinkedList<>();
 21:         myWords.add("ten");
 22:         myWords.add("twenty");
 23:         myWords.add("Thirty");
 24:         myWords.add("forty");
 25:         myWords.add("Fifty");
 26:         myWords.add("twenty");
 27: 
 28:         // add some values (not on the end)
 29:         myWords.add(1, "fifteen");
 30:         myWords.add(5, "forty-five");
 31:         
 32:         // show the original
 33:         System.out.println("I have created a list of words.  Here they are:");
 34:         printList(myWords);
 35: 
 36:         // show the list with apologies for F-words
 37:         System.out.println("Here they are with apologies for the f-words:");
 38:         flagFWords(myWords);
 39:         printList(myWords);
 40: 
 41:         // show the list sorted with F-words beeped out
 42:         System.out.println("Here they are with the f-words beeped out:");
 43:         beepFWords(myWords);
 44:         printList(myWords);
 45: 
 46:         // show the list sorted with Beeps deleted
 47:         System.out.println("Here they are with the beeps deleted:");
 48:         deleteBeeps(myWords);
 49:         printList(myWords);
 50: 
 51:         // show the list sorted into lexicographic order
 52:         System.out.println("Here they are sorted into LEXICOGRAPHIC order:");
 53:         Collections.sort(myWords);
 54:         printList(myWords);
 55: 
 56:         // show the list sorted into alphabetical order
 57:         System.out.println("Here they are sorted into ALPHABETICAL order:");
 58:         Collections.sort(myWords, String.CASE_INSENSITIVE_ORDER);
 59:         printList(myWords);
 60:     }
 61:     
 62:     /**
 63:      * Print a List of Strings four per line.
 64:      *
 65:      * @param theList the list to be printed.
 66:      */
 67:     public static void printList(List<String> theList) {
 68:         for (int i = 0; i < theList.size(); ++i) {
 69:             if (i % 4 == 0) {
 70:                 System.out.println();
 71:             }
 72:             System.out.printf("%15s", theList.get(i));
 73:         }
 74:         System.out.println("\n");
 75:     }
 76: 
 77:     /**
 78:      * Add an apology after each F- (or f-) word.
 79:      *
 80:      * @param theList the list to have f-words flagged.
 81:      */
 82:     public static void flagFWords(List<String> theList) {
 83:         ListIterator<String> it = theList.listIterator();
 84:         while (it.hasNext()) {
 85:             String x = it.next();
 86:             if (x.startsWith("f") || x.startsWith("F")) {
 87:                 it.add("(sorry)");
 88:             }   
 89:         }
 90:     }    
 91: 
 92:     /**
 93:      * Change all words that start with F (or f) to "BEEP". 
 94:      *
 95:      * @param theList the list to have f-words beeped out.
 96:      */
 97:     public static void beepFWords(List<String> theList) {
 98:         ListIterator<String> it = theList.listIterator();
 99:         while (it.hasNext()) {
100:             String x = it.next();
101:             if (x.startsWith("f") || x.startsWith("F")) {
102:                 it.set("BEEP");
103:             }
104:         }
105:     }
106: 
107:     /**
108:      * Remove all "BEEP"s from a list of Strings.
109:      *
110:      * @param theList the list to remove all BEEPs from.
111:      */
112:     public static void deleteBeeps(List<String> theList) {
113:         ListIterator<String> it = theList.listIterator();
114:         while (it.hasNext()) {
115:             String x = it.next();
116:             if (x.equals("BEEP")) {
117:                 it.remove();
118:             }
119:         }
120:     }
121: 
122: }