Download presentation
Presentation is loading. Please wait.
Published byGiles Neal Modified over 9 years ago
1
CS 2430 Day 26
2
Announcements Exam #2: Wednesday, April 3 –Review in lab on Tuesday, April 2 –Sample problems sent via email
3
Agenda Generic Queue
4
Generic types
5
How to make a generic container class in Java?
6
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?
7
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.
8
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
9
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
10
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
11
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
12
Implementation
13
Constructor public Queue() { items = ??? }
14
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[] ).
15
Constructor fixed! public Queue() { items = (E[]) new Object[DEFAULT_MAX]; } We might get a “warning” from the compiler. We’ll just live with it.
16
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]; }
17
The rest is the same, but just replace Object with E
18
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
19
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
20
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!
21
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!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.