CSC300: Generic classes [7/9] Previous pageContentsNext page

A generic class has a type parameter. Type is not stored at runtime.

file:XFixedCapacityStack.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package algs13;
import stdlib.*;
public class XFixedCapacityStack<T> {
  private T[] a; // holds the items
  private int N; // number of items in stack

  @SuppressWarnings("unchecked")
  public XFixedCapacityStack (int capacity) {
    this.a = (T[]) new Object[capacity]; // no generic array creation
    this.N = 0;
  }
  public int size ()        { return N; }
  public boolean isEmpty () { return (N == 0); }
  public void push (T item) {
    if (item == null) throw new IllegalArgumentException ();
    if (N >= a.length) throw new RuntimeException ("Stack full");
    a[N] = item;
    N++;
  }
  public T pop () {
    if (N <= 0) throw new RuntimeException ("Stack empty");
    N--;
    T result = a[N];
    a[N] = null;
    return result;
  }

  public static void main (String[] args) {
    //Trace.showObjectIdsRedundantly (true);
    Trace.showBuiltInObjects (true);
    //Trace.showBuiltInObjectsVerbose (true);
    Trace.drawStepsOfMethod ("main");
    Trace.run ();
    
    XFixedCapacityStack<Integer> s1 = new XFixedCapacityStack<> (5);
    XFixedCapacityStack<String> s2 = new XFixedCapacityStack<> (3);
    s1.push (11);
    s1.push (21);
    s1.push (31);

    //s2.push (41);
    s2.push ("duck");
    s2.push ("goose");
  }
}
XFixedCapacityStack_main_44

Previous pageContentsNext page