/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package generictypes;
/**
*
* @author Capn_Clunks
*/
public class GenericTypes
{
public class Queue<E>//Inbetween <> denotes a generic type
{
private E[] elements;//[]Denotes a array type
private int head, tail;
@SuppressWarnings("unchecked")
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;
}
boolean isFull()
{
return (tail +1)%elements.length == head;
}
boolean isEmpty()
{
return head == tail;
}
E remove() throws QueueEmptyException
{
if(isEmpty())
{
throw new QueueEmptyException();
}
E element = elements[head];
head = (head + 1) % elements.length;
return element;
}
}
public static void main(String[] args)
throws QueueFullException, QueueEmptyException
{
Queue<String> queue = new Queue<String>(6);//This is the offender
System.out.println("Empty: " + queue.isEmpty());
}
}
Copied directly out of a book so should work, confused as to why because I can't make static and I just wanted to compile as a example to myself to demonstrate the concept.