Java Reference
In-Depth Information
Note Javaallowsrawtypestobeintermixedwithgenerictypestosupportthevast
amount of legacy code that was written prior to the arrival of generics. However, the
Declaring and Using Your Own Generic Types
It is not difficult to declare your own generic types. In addition to specifying a formal
plementation. For example, Listing 3-52 declares a Queue<E> generic type.
Listing 3-52. Declaring and using a Queue<E> generic type
class Queue<E>
private E[] elements;
private int head, tail;
Queue(int size)
if (size < 2)
throw new IllegalArgumentException(""+size);
elements = (E[]) new Object[size];
head = 0;
tail = 0;
void insert(E element) throws QueueFullException
if (isFull())
throw new QueueFullException();
elements[tail] = element;
tail = (tail+1)%elements.length;
E remove() throws QueueEmptyException
if (isEmpty())
throw new QueueEmptyException();
E element = elements[head];
Search WWH ::

Custom Search