CS 2430 Day 26
Announcements Exam #2: Wednesday, April 3 –Review in lab on Tuesday, April 2 –Sample problems sent via
Agenda Generic Queue
Generic types
How to make a generic container class in Java?
First try Make a different class to hold each type of Object We would have BagOfDate, BagOfString, etc. Problem? Too much similarity! Why write all that code?
Second try Make containers backed by array of Object Can hold any type of Object Problem? Casting required to “get” items Example: Rational rat = (Rational)operands.pop(); Awkward: Container could hold Rational s, String s, Date s, etc.
Java generics Can make a generic container class that is instantiated with a particular type (of Object ) The “type” is a parameter to the class The “type” parameter MUST be a class, NOT a primitive type
public class Queue { private static final int DEFAULT_MAX = 10; private Object[] items; private int front, rear, count; public Queue() {... } public Queue(int inSize) {... } public boolean isEmpty() {... } public boolean isFull() {... } public void enqueue(Object obj) {... } public Object dequeue() {... } } Not generic! Not generic Queue
public class Queue // E must be a class, not a primitive { private static final int DEFAULT_MAX = 10; private E[] items; private int front, rear, count; public Queue() {... } public Queue(int inSize) {... } public boolean isEmpty() {... } public boolean isFull() {... } public void enqueue(E obj) {... } public E dequeue() {... } } Generic Queue
public class Queue // E must be a class, not a primitive { private static final int DEFAULT_MAX = 10; private E[] items; private int front, rear, count; public Queue() {... } public Queue(int inSize) {... } public boolean isEmpty() {... } public boolean isFull() {... } public void enqueue(E obj) {... } public E dequeue() {... } } Same as before, but with Object replaced by E Generic Queue
Implementation
Constructor public Queue() { items = ??? }
Does this work? public Queue() { items = new E[DEFAULT_MAX]; } Java doesn’t allow this! We need to create an array of Object and cast it to ( E[] ).
Constructor fixed! public Queue() { items = (E[]) new Object[DEFAULT_MAX]; } We might get a “warning” from the compiler. We’ll just live with it.
Constructors public Queue() { items = (E[]) new Object[DEFAULT_MAX]; } public Queue(int inSize) { if (inSize < 0) items = (E[]) new Object[DEFAULT_MAX]; else items = (E[]) new Object[inSize]; }
The rest is the same, but just replace Object with E
Client code Queue dq = new Queue (365); Queue rq = new Queue (40); Date date = dq.dequeue(); // no cast Rational rat = rq.dequeue(); rq.enqueue(new Rational(1, 2)); // same as before
Can also do this Queue dq = new Queue(365); Queue rq = new Queue(40); Date date = dq.dequeue(); // no cast Rational rat = rq.dequeue(); rq.enqueue(new Rational(1, 2)); // same as before
Can’t do this! Queue dq = new Queue(365); Queue rq = new Queue(40); Date date = dq.dequeue(); Rational rat = rq.dequeue(); rq.enqueue(new Rational(1, 2)); dq.enqueue(rat); // this is a syntax error!
Or this! Queue dq = new Queue(365); Queue rq = new Queue(40); Date date = dq.dequeue(); Rational rat = rq.dequeue(); rq.enqueue(new Rational(1, 2)); rat = dq.dequeue(); // this is a syntax error!