



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




2
3
A few concepts that are broadly useful Not an exhaustive set of useful concepts
Interfaces (i.e., ADTs) Implementations
4
class ArrayStack implements Stack {
private Object[] array; //Array that holds the Stack private int index = 0; //First empty slot in Stack
public ArrayStack (int maxSize) { array = new Object[maxSize]; }
public void push(Object x) { array[index++] = x; } public Object pop() { return array[--index]; } public Object peek() { return array[index-1]; } public boolean isEmpty() { return index == 0; } public void makeEmpty() { index = 0; } }
class TestArrayStack { public static void main( String [] args ) { ArrayStack stack = new ArrayStack(100); stack.push( new Integer(17) ); stack.push( “okay” );
String str = (String) stack.pop(); // downcast works String bad = (String) stack.pop(); // runtime error! }
7
8
//removes 4-letter words from c //elements must be Strings static void purge(Collection c) { Iterator i = c.iterator(); while (i.hasNext()) { if (((String)i.next()).length() == 4) i.remove(); }}
//removes 4-letter words from c static void purge(Collection
9
Map grades = new HashMap(); grades.put("John",new Integer(67)); grades.put("Jane",new Integer(88)); grades.put("Fred",new Integer(72)); Integer x = (Integer)grades.get("John"); sum = sum + x.intValue();
Map<String,Integer> grades = new HashMap<String,Integer>(); grades.put("John",new Integer(67)); grades.put("Jane",new Integer(88)); grades.put("Fred",new Integer(72)); Integer x = grades.get("John"); sum = sum + x.intValue();
10
Map<String,Integer> grades = new HashMap<String,Integer>(); grades.put("John",new Integer(67)); grades.put("Jane",new Integer(88)); grades.put("Fred",new Integer(72)); Integer x = grades.get("John"); sum = sum + x.intValue();
Map<String,Integer> grades = new HashMap<String,Integer>(); grades.put("John",new Integer(67)); grades.put("Jane",new Integer(88)); grades.put("Fred",new Integer(72)); Integer x = grades.get("John"); sum = sum + x.intValue());
Map<String,Integer> grades = new HashMap<String,Integer>(); grades.put("John", 67); grades.put("Jane", 88); grades.put("Fred", 72); sum = sum + grades.get("John");
19
static void a2c(Object[] a, Collection<?> c) { for (Object o : a) { c.add(o); // compile time error }}
static
20
21
22
public boolean containsAll(Collection<?> c); public boolean addAll(Collection<? extends E> c); public boolean removeAll(Collection<?> c); public boolean retainAll(Collection<?> c); public void clear();
25
26
public HashSet(); public HashSet(Collection<? extends E> c); public HashSet(int initialCapacity); public HashSet(int initialCapacity, float loadFactor);
public TreeSet(); public TreeSet(Collection<? extends E> c); ...
27
28
public TreeSet(); public TreeSet(Collection<? extends E> c); public TreeSet(Comparator<? super E> comparator); ...