xtremeslice
xtremeslice

Reputation: 27

Making a small queue (Generics)

Hi I'm trying to make a queue that would refactor my code to remove Eclipses error message: "GGCQueue is a raw type. References to generic type GGCQueue should be parameterized". I want to make a queue that would never contain more than 10 elements. I am trying to do this following 'Generic' principles but at the moment I cannot figure out how to do that. I have the following code (in the class GGCQueue the constructor GGCQueue() is where I need to implement this):

import java.util.LinkedList;
import java.util.List;

public class GenericsProblem {

    public static void main(String[] args) {

        GGCQueue ggcQ = new GGCQueue();
        ggcQ.offer(100);
        ggcQ.offer(1000);
        ggcQ.offer(3.33D);
        ggcQ.offer(9);
        ggcQ.offer(7);  
        ggcQ.offer(9.001F);

        System.out.println("polling the queue, expecting 100, got:" + ggcQ.poll());
        System.out.println("polling the queue, expecting 1000, got:" + ggcQ.poll());
        System.out.println("polling the queue, expecting 3.33, got:" + ggcQ.poll());
        System.out.println("polling the queue, expecting 9, got:" + ggcQ.poll());
        System.out.println("polling the queue, expecting 7, got:" + ggcQ.poll());
        System.out.println("polling the queue, expecting 9.001, got:" + ggcQ.poll());

    }

}


class GGCQueue<E> {

    List<E> q = new LinkedList<E>();

    public GGCQueue() {
    //TODO MAKE THE SMALL QUEUE <= 10 ELEMENTS
    }

    public void offer(E element) {
        if (q.size() == 10)
            return;
        q.add(element);
    }

    public E poll() {
        E element = q.remove(0);
        return element;
    }

}

Upvotes: 0

Views: 43

Answers (1)

AJNeufeld
AJNeufeld

Reputation: 8695

Looks like you want:

 GGCQueue<Number> ggcQ = new GGCQueue<>();

Upvotes: 1

Related Questions