Source of MoreArrayListMethods.java


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

  5: /**
  6:  * A program demonstrating more modifications on Lists.
  7:  *
  8:  * @author Mark Young (A00000000)
  9:  */
 10: public class MoreArrayListMethods {

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

 14:     public static void main(String[] args) {
 15:         List<String> names1 = new ArrayList<>();
 16:         List<String> list1, list2;

 18:         // List basics -- create and add
 19:         System.out.println("\nLists start empty.");
 20:         System.out.println("\tnames1: " + names1);
 21:         System.out.println("\tnames1.isEmpty(): " + names1.isEmpty());
 22:         System.out.println("After we add elements, they aren't empty.");
 23:         names1.add("Alex");
 24:         names1.add("Betty");
 25:         names1.add("Carol");
 26:         names1.add("David");
 27:         System.out.println("\tnames1: " + names1);
 28:         System.out.println("\tnames1.isEmpty(): " + names1.isEmpty());
 29:         pause();

 31:         // copy and extend
 32:         System.out.println("\nNow some manipulations:");
 33:         list1 = new ArrayList<>(names1);  // copy list1
 34:         list2 = new ArrayList<>();
 35:         list2.add("Andrew");
 36:         list2.add("Betty");
 37:         list2.add("Chris");
 38:         System.out.println("list1.addAll(list2)");
 39:         System.out.println("\tlist1 starts as: " + list1);
 40:         System.out.println("\tlist2 starts as: " + list2);
 41:         list1.addAll(list2);
 42:         System.out.println("\tlist1 ends as: " + list1);
 43:         System.out.println("\tlist2 ends as: " + list2);
 44:         pause();

 46:         // removeAll removes multiple values from a List
 47:         list1 = new ArrayList<>(names1);  // restore list1 to old value
 48:         System.out.println("list1.removeAll(list2)");
 49:         System.out.println("\tlist1 starts as: " + list1);
 50:         System.out.println("\tlist2 starts as: " + list2);
 51:         list1.removeAll(list2);
 52:         System.out.println("\tlist1 ends as: " + list1);
 53:         System.out.println("\tlist2 ends as: " + list2);
 54:         pause();

 56:         // retainAll keeps only acceptable values in a List.
 57:         list1 = new ArrayList<>(names1);  // restore list1 to old value
 58:         System.out.println("list1.retainAll(list2)");
 59:         System.out.println("\tlist1 starts as: " + list1);
 60:         System.out.println("\tlist2 starts as: " + list2);
 61:         list1.retainAll(list2);
 62:         System.out.println("\tlist1 ends as: " + list1);
 63:         System.out.println("\tlist2 ends as: " + list2);
 64:         pause();

 66:         // clear removes all elements from a List.
 67:         list1 = new ArrayList<>(names1);  // restore list1 to old value
 68:         System.out.println("list1.clear()");
 69:         System.out.println("\tlist1 starts as: " + list1);
 70:         list1.clear();
 71:         System.out.println("\tlist1 ends as: " + list1);
 72:         pause();

 74:         // containsAll check whether multiple elements are in a List.
 75:         list1 = new ArrayList<>(names1);  // restore list1 to old value
 76:         System.out.println("list1.containsAll(list2)");
 77:         System.out.println("\tlist1 is: " + list1);
 78:         System.out.println("\tlist2 is: " + list2);
 79:         System.out.println("\tlist1.containsAll(list2) is: "
 80:                 + list1.containsAll(list2));
 81:         pause();

 83:         // subList picks out part of a List.
 84:         list1 = new ArrayList<>(names1);  // restore list1 to old value
 85:         System.out.println("list1.subList(2, 4)");
 86:         System.out.println("\tlist1 is: " + list1);
 87:         System.out.println("\tlist1.subList(2, 4) is: " + list1.subList(2, 4));
 88:         System.out.println("\tlist1.subList(2, 4).get(0) is: "
 89:                 + list1.subList(2, 4).get(0));
 90:         pause();

 92:         // changes to parts of a subList are reflected in the original list!
 93:         System.out.println("list2 = list1.subList(2, 4)");
 94:         list2 = list1.subList(2, 4);
 95:         System.out.println("\tlist1 is: " + list1);
 96:         System.out.println("\tlist2 is: " + list2);
 97:         System.out.println("list2.set(1, \"CHANGED IN list2\")");
 98:         list2.set(1, "CHANGED IN list2");
 99:         System.out.println("\tlist1 is: " + list1);
100:         System.out.println("\tlist2 is: " + list2);
101:         System.out.println("list2.clear()");
102:         list2.clear();
103:         System.out.println("\tlist1 is: " + list1);
104:         System.out.println("\tlist2 is: " + list2);
105:         pause();
106:     }

108:     /**
109:      * Prompt the user and wait for them to press the enter key.
110:      */
111:     public static void pause() {
112:         System.out.print("Press Enter...");
113:         kbd.nextLine();
114:         System.out.println();
115:     }

117: }