Upload
giles-neal
View
215
Download
0
Embed Size (px)
Citation preview
CS 2430
Day 26
Announcements
• Exam #2: Wednesday, April 3– Review in lab on Tuesday, April 2– Sample problems sent via email
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 Rationals, Strings, Dates, 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> // 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> // 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
Constructorpublic 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.
Constructorspublic 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 codeQueue<Date> dq = new Queue<Date>(365);
Queue<Rational> rq = new Queue<Rational>(40);
Date date = dq.dequeue(); // no cast
Rational rat = rq.dequeue();
rq.enqueue(new Rational(1, 2)); // same as before
Can also do thisQueue<Date> dq = new Queue(365);
Queue<Rational> 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<Date> dq = new Queue(365);
Queue<Rational> 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<Date> dq = new Queue(365);
Queue<Rational> 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!