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.
- format and printf
- ArrayList Methods
- HashSet Methods
- HashMap Methods
- Collections Methods
- format and printf
- 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 |
- 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
- 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
-
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
- 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
- ArrayList Methods
- add
list.add(value);
// add value to end of list
list.add(index, value); // add value at the given index
- addAll
list.addAll(otherList); // add all elements from another list
list.addAll(index, otherList); // add all elements at the given index
- clear
list.clear();
// clear the list (remove all elements from it)
- contains
list.contains(value) // return true if the list contains the value,
false otherwise
- get
list.get(index); // return the element at the given index
- indexOf
list.indexOf(value); // return first index of given value, or -1 if not
in list
- isEmpty
list.isEmpty(); //
return true if list is empty (size 0), or false otherwise
- lastIndexOf
list.lastIndexOf(value); // return last index of given value, or -1 if
not in list
- remove
list.remove(value); // remove first occurrence of value from list
list.remove(index); // remove element at the given index from list
- set
list.set(index, value); // set element at given index to value
- size
list.size();
// return the size of the list (# of elements in the list)
- HashSet Methods
- add
set.add(value); //
add value to the set if not already present
- addAll
set.addAll(list);
// add all elements from the list to the set
- clear
set.clear();
// clear the set (remove all elements from it)
- contains
set.contains(value)
// return true if the set contains the value, false otherwise
- isEmpty
set.isEmpty(); //
return true if set is empty (size 0), or false otherwise
- remove
set.remove(value);
// remove value from set
- size
set.size();
// return the size of the set (# of elements in the set)
- HashMap Methods
- clear
map.clear(); //
remove all key-value entries from the hashMap
- get
map.get(key); //
return the value this key maps to, or null if not in map.
- isEmpty
map.isEmpty(); //
return true if the map is empty (size 0), false otherwise
- keySet
map.keySet(); //
return a Set containing all the keys in this map
- put
map.put(key,
value); // map the given key to the given value
- remove
map.remove(key); //
remove the key (and its mapped value, if any) from the map
- size
map.size();
// return the size of the map (# of key-value pairs in the map)
- Collections Methods
- 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.
- reverse
Collections.reverse(list); // Reverse the list, so [1, 2, 3] becomes [3,
2, 1]
- 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]
- shuffle
Collections.shuffle(list); // Shuffle the list into a random order
- 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