toad
Fall 2013
Jonathan Aldrich
School of
Computer Science
Charlie Garrod
Administrivia
Midterm exam in class next Tuesday, 15 Oct
Midterm review session Sunday 1 3 p.m.,
Hamburg Hall 1000
Review sheet will be released before the review
Will also release a sample midterm exam soon
15-214 Garrod
session
15-214 Garrod
The
The
Model-View-Controller pattern
Observer pattern
Threading architecture
Swing components
Using the Observer pattern
15-214 Garrod
in Swing
Composite
Template Method
Strategy
Observer
Decorator
Model-View-Controller
15-214 Garrod
of the features
of the common usage patterns
of the design patterns in use
Iterator, Marker Interface, Factory Method, Adapter,
Strategy, Decorator, Template Method
15-214 Garrod
15-214 Garrod
add(E e);!
boolean
addAll(Collection<E> c);!
boolean
remove(E e);!
boolean
removeAll(Collection<E> c);!
boolean
retainAll(Collection<E> c);!
boolean
contains(E e);!
boolean
containsAll(Collection<E> c);!
void
clear();!
int
size();!
boolean
isEmpty();!
Iterator<E> iterator();!
Object[]
toArray()!
E[]
toArray(E[] a);!
15-214 Garrod
15-214 Garrod
15-214 Garrod
10
15-214 Garrod
add(E e);
remove();
element();
offer(E e);!
poll();
// These two methods!
peek();
// might return null!
11
15-214 Garrod
12
the toArray
List<String>
String[] arr
String[] brr
method
arguments = new LinkedList<String>();!
// puts something into the list!
= (String[]) arguments.toArray();!
= arguments.toArray(new String[0]);
15-214 Garrod
13
the toArray
List<String>
String[] arr
String[] brr
method
arguments = new LinkedList<String>();!
// puts something into the list!
= (String[]) arguments.toArray();!
= arguments.toArray(new String[0]);
you want
Solution: Expose the functionality of the object in a different
form
!
15-214
Garrod
14
15-214 Garrod
15
Traversing a Collection
Old-school Java for loop for ordered types
List<String> arguments = ;!
for (int i = 0; i < arguments.size(); ++i) {!
System.out.println(arguments.get(i));!
}!
Use an Iterator
15-214 Garrod
16
To use, e.g.:
List<String> arguments = ; !
for (Iterator<String> it = arguments.iterator();!
it.hasNext(); ) {!
String s = it.next();!
System.out.println(s);!
}!
15-214 Garrod
17
Using a java.util.Iterator<E>
public interface Iterator<E> {!
boolean hasNext();!
E next();!
void remove(); // removes previous returned item !
}
// from the underlying collection!
18
}!
15-214 Garrod
19
}!
15-214
Garrod
20
Sorting a Collection
Use the Collections.sort method:
public static void main(String[] args) {!
List<String> lst = Arrays.asList(args);!
Collections.sort(lst);!
for (String s : lst) {!
System.out.println(s);!
}!
}!
21
15-214 Garrod
22
15-214 Garrod
23
{!
// should!
// check!
// for null!
24
25
Alternative comparisons
public class Employee implements Comparable<Employee> {!
protected Name name;!
protected int salary;!
!
}
15-214 Garrod
26
Alternative comparisons
public class Employee implements Comparable<Employee> {!
protected Name name;!
protected int salary;!
!
}
15-214 Garrod
27
28
Using a Comparator
Order-dependent classes and methods take a
Comparator as an argument
public class Main {!
public static void main(String[] args) {!
SortedSet<Employee> empByName =
// sorted by name!
new TreeSet<Employee>();!
!
SortedSet<Employee> empBySal = // sorted by salary!
new TreeSet<Employee>(new EmpSalComp());!
}!
}!
15-214 Garrod
29
comparator();!
first();!
last();!
subSet(E fromElement, E toElement);!
headSet(E toElement);!
tailSet(E fromElement);
15-214 Garrod
30
15-214 Garrod
31
15-214 Garrod
32
15-214 Garrod
List<T>
Set<T>
Map<K,V>
List<T>
Set<T>
Map<K,V>
unmodifiableList(List<T>
unmodifiableSet( Set<T>
unmodifiableMap( Map<K,V>
synchronizedList(List<T>
synchronizedSet( Set<T>
synchronizedMap( Map<K,V>
.!
.!
.
lst);!
set);!
map);!
lst);!
set);!
map);!
33
15-214 Garrod
34
15-214 Garrod
35
get(int i);
// Template Method.!
size();
// Template Method.!
set(int i, E e);
// set add remove are!
add(E e);
// pseudo-abstract!
remove(E e);
// Template Methods.!
addAll(Collection<E> c);!
removeAll(Collection<E> c);!
retainAll(Collection<E> c);!
contains(E e);!
containsAll(Collection<E> c);!
clear();!
isEmpty();!
iterator();!
toArray()!
toArray(E[] a);!
36
Next time
Midterm exam on Tuesday
15-214 Garrod
37