Source of CollectionsDemo.java


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

  5: /**
  6:  * Demonstration of some Collections methods with Lists.
  7:  *
  8:  * @author Mark Young (A00000000)
  9:  */
 10: public class CollectionsDemo {

 12:     public static void main(String[] args) {
 13:         // introduce yourself
 14:         System.out.println("A program to show off some Collections methods.");
 15:         System.out.println();

 17:         // create the list of numbers
 18:         List<Integer> list = new ArrayList<Integer>();
 19:         int[] numArr = new int[] {50, 19, 21, 44, 18, 21};
 20:         for (int i = 0; i < numArr.length; i++) {
 21:             list.add(numArr[i]);
 22:         }

 24:         // print out list and values of Collections methods
 25:         System.out.println("list is " + list);
 26:         System.out.println("\tCollections.max(list) is "
 27:                 + Collections.max(list));
 28:         System.out.println("\tCollections.min(list) is "
 29:                 + Collections.min(list));

 31:         // print out list modified by Collections methods
 32:         System.out.println("Collections.reverse(list)");
 33:         Collections.reverse(list);
 34:         System.out.println("\tlist is now " + list);
 35:         System.out.println("Collections.sort(list)");
 36:         Collections.sort(list);
 37:         System.out.println("\tlist is now " + list);
 38:         System.out.println("Collections.swap(list, 1, 3)");
 39:         Collections.swap(list, 1, 3);
 40:         System.out.println("\tlist is now " + list);
 41:         System.out.println("Collections.replaceAll(list, 0, 21)");
 42:         Collections.replaceAll(list, 0, 21);
 43:         System.out.println("\tlist is now " + list);
 44:         System.out.println("Collections.replaceAll(list, 21, 0)");
 45:         Collections.replaceAll(list, 21, 0);
 46:         System.out.println("\tlist is now " + list);
 47:         System.out.println("Collections.shuffle(list)");
 48:         Collections.shuffle(list);
 49:         System.out.println("\tlist is now " + list);
 50:         System.out.println("Collections.shuffle(list)");
 51:         Collections.shuffle(list);
 52:         System.out.println("\tlist is now " + list);
 53:     }

 55: }