Source of MoreModification.java


  1: import java.util.ArrayList;
  2: import java.util.List;
  3: import java.util.ListIterator;

  5: /**
  6:  * A program showing the proper way to change lists (using a ListIterator).
  7:  *
  8:  * @author Mark Young (A00000000)
  9:  */
 10: public class MoreModification {

 12:     public static void main(String[] args) {
 13:         List<String> original = new ArrayList<>();
 14:         original.add("Ten");
 15:         original.add("Fifteen");
 16:         original.add("Twenty");
 17:         original.add("Thirty");
 18:         original.add("Fifty");
 19:         System.out.println("Original list: " + original);

 21:         // we can make a COPY of a list!
 22:         List<String> first = new ArrayList<>(original);
 23:         ListIterator<String> it1 = first.listIterator();
 24:         while (it1.hasNext()) {
 25:             String word = it1.next();
 26:             if (word.startsWith("F")) {
 27:                 it1.remove();
 28:             }
 29:         }
 30:         System.out.println("F-words removed: " + first);

 32:         // we can make a COPY of a list!
 33:         List<String> second = new ArrayList<>(original);
 34:         ListIterator<String> it2 = second.listIterator();
 35:         while (it2.hasNext()) {
 36:             String word = it2.next();
 37:             if (word.startsWith("F")) {
 38:                 it2.set(word.toUpperCase());
 39:             }
 40:         }
 41:         System.out.println("F-words in ALL_CAPS: " + second);

 43:         // we can make a COPY of a list!
 44:         List<String> third = new ArrayList<>(original);
 45:         ListIterator<String> it3 = third.listIterator();
 46:         while (it3.hasNext()) {
 47:             String word = it3.next();
 48:             if (word.startsWith("F")) {
 49:                 it3.add(word.toLowerCase());
 50:             }
 51:         }
 52:         System.out.println("F-words duplicated: " + third);
 53:     }

 55: }