Computer Science 15-100, Fall 2008
Class Notes:  Quiz 7-8 Handout


These excerpts from this week's notes will be provided to students during Quiz 7 and Quiz 8.


  1. format and printf
  2. ArrayList Methods
  3. HashSet Methods
  4. HashMap Methods
  5. Collections Methods

  1. format and printf
     
    1. The Conversion Types

      %[flags][width][.precision]conversion

      b boolean
      d decimal integer
      o octal integer
      h hex integer
      H Hex integer
      f Floating-point number
      e Floating-point number in scientific notation
      g Floating-point number in compact form
      s String

       

    2. format and printf Examples

         // convert to hexadecimal
         int y = 165;
         System.out.printf("%d\n",y); // 165
         System.out.printf("%h\n",y); // a5
         System.out.printf("%H\n",y); // A5

         // different forms of floating-point numbers
         double d = Math.pow(Math.PI,20);
         System.out.printf("%e\n",d); // 8.769957e+09
         System.out.printf("%f\n",d); // 8769956796.082693
         System.out.printf("%g\n",d); // 8.76996e+09
       
    3. Field width

         int y = 123, z = 45;
         System.out.printf("123456789\n");  // 123456789
         System.out.printf("%4d%4d\n",y,z); //  123  45
         System.out.printf("%1d%4d\n",y,z); // 123  45
       
    4. Flags:  Left-Justified ('-'), Use-Sign ('+'), and Zero-Padded ('0')

         int y = 123, z = 45;
         System.out.printf("123456789\n");    // 123456789
         System.out.printf("%4d%+4d\n",y,z);  //  123 +45
         System.out.printf("%-4d%+4d\n",y,z); // 123  +45
         System.out.printf("%+05d%4d\n",y,z); // +0123  45
       
    5. Precision

         double d = 45.678;
         System.out.printf("%.0f\n",d);    // 46
         System.out.printf("%.1f\n",d);    // 45.7
         System.out.printf("%.2f\n",d);    // 45.68
         System.out.printf("%+06.2f\n",d); // +45.68
         System.out.printf("%+07.2f\n",d); // +045.68

       
  2. ArrayList Methods
     
    1. add
      list.add(value);        // add value to end of list
      list.add(index, value); // add value at the given index
       
    2. addAll
      list.addAll(otherList);        // add all elements from another list
      list.addAll(index, otherList); // add all elements at the given index

       
    3. clear
      list.clear(); // clear the list (remove all elements from it)
       
    4. contains
      list.contains(value)  // return true if the list contains the value, false otherwise
       
    5. get
      list.get(index);  // return the element at the given index
       
    6. indexOf
      list.indexOf(value); // return first index of given value, or -1 if not in list
       
    7. isEmpty
      list.isEmpty();  // return true if list is empty (size 0), or false otherwise
       
    8. lastIndexOf
      list.lastIndexOf(value); // return last index of given value, or -1 if not in list
       
    9. remove
      list.remove(value);  // remove first occurrence of value from list
      list.remove(index);  // remove element at the given index from list

       
    10. set
      list.set(index, value); // set element at given index to value
       
    11. size
      list.size(); // return the size of the list (# of elements in the list)
       
  3. HashSet Methods
     
    1. add
      set.add(value);  // add value to the set if not already present
       
    2. addAll
      set.addAll(list); // add all elements from the list to the set
       
    3. clear
      set.clear(); // clear the set (remove all elements from it)
       
    4. contains
      set.contains(value)  // return true if the set contains the value, false otherwise
       
    5. isEmpty
      set.isEmpty();  // return true if set is empty (size 0), or false otherwise
       
    6. remove
      set.remove(value);  // remove value from set
       
    7. size
      set.size(); // return the size of the set (# of elements in the set)
       
  4. HashMap Methods
     
    1. clear
      map.clear(); // remove all key-value entries from the hashMap
       
    2. get
      map.get(key); // return the value this key maps to, or null if not in map.
       
    3. isEmpty
      map.isEmpty(); // return true if the map is empty (size 0), false otherwise
       
    4. keySet
      map.keySet(); // return a Set containing all the keys in this map
       
    5. put
      map.put(key, value); // map the given key to the given value
       
    6. remove
      map.remove(key); // remove the key (and its mapped value, if any) from the map
       
    7. size
      map.size(); // return the size of the map (# of key-value pairs in the map)
       
  5. Collections Methods
     
    1. binarySearch
      Collections.binarySearch(list, value); // Assuming list is sorted, perform binary
                                             // search and return the index of the value
                                             // or -1 if it is not in the list.

       
    2. reverse
      Collections.reverse(list); // Reverse the list, so [1, 2, 3] becomes [3, 2, 1]
       
    3. rotate
      Collections.rotate(list, distance); // Rotate the list to the right by the given
                                          // distance.  So rotating [1, 2, 3, 4] two to
                                          // the right becomes [3, 4, 1, 2]
    4. shuffle
      Collections.shuffle(list); // Shuffle the list into a random order
       
    5. sort
      Collections.sort(list); // Sort the list

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem