1

I'm confused as to what I am doing wrong with this implementation of AbstractQueue.

This code is what is causing the error:

public static void main(String[] args) {
    Stack<Integer> intStack = new Stack();
    MyQueue<Integer> realQueue = new MyQueue();
    PriorityQueue<Integer> intQueue = new PriorityQueue();
}

Eclipse is saying I need to remove the generic typing from MyQueue.

Here is the class as implemented:

import java.util.AbstractQueue;
import java.util.Iterator;

public class MyQueue extends AbstractQueue<Object> {

    private T[] arr;
    private int headPos;
    private int tailPos;
    private int size = 0;

    @Override
    public boolean offer(T e) {
        if (size < arr.length)
            size++;
        else if (headPos == tailPos)
            headPos = nextPos(headPos);
        arr[tailPos] = e;
        tailPos = nextPos(tailPos);
        return true;
    }

    private int nextPos(int pos) {
        return (pos + 1) % arr.length;
    }

    @Override
    public T peek() {
        if (size == 0)
            return null;
        return arr[headPos];
    }

    @Override
    public T poll() {
        if (size == 0)
            return null;
        size--;
        T res = arr[headPos];
        headPos = nextPos(headPos);
        return res;
    }

    @Override
    public Iterator iterator() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int size() {
        return size;
    }
}

This is both my first time implementing a queue, and my first time creating a class to work with generics, so I am thoroughly lost as to what to do. When I follow Eclipse's advice, I get a null pointer exception, and when I leave the generic code in, it does not compile.

7
  • Your are extending AbstractQueue<Object>, there is no generic Commented May 30, 2013 at 1:03
  • 1
    You should declare your class public class MyQueue<T> extends AbstractQueue<T>. Commented May 30, 2013 at 1:03
  • ^ And replace all Object with T Commented May 30, 2013 at 1:04
  • changed that, still gives me a null pointer exception when I try to add something to the queue. Commented May 30, 2013 at 1:07
  • Your class declaration should be public class MyQueue<T> extends AbstractQueue<T>. Your variable declarations need to specify the generic parameter type on the RHS - Stack<Integer> intStack = new Stack<Integer>();. And your NPE is coming from somewhere else in your code. Commented May 30, 2013 at 1:08

1 Answer 1

1

I think you were not using generics properly so I have modified your class. Please check if it works for you.....
UPDATE: I have changed the code a bit to instantiate the arr[] array. At the bottom main method and a constructor have been added.

import java.util.AbstractQueue;
import java.util.Iterator;

public class MyQueue<T> extends AbstractQueue<T> {

    private T[] arr;
    private int headPos;
    private int tailPos;
    private int size = 0;

    @Override
    public boolean offer(T e) {
        if (size < arr.length)
            size++;
        else if (headPos == tailPos)
            headPos = nextPos(headPos);
        arr[tailPos] = e;
        tailPos = nextPos(tailPos);
        return true;
    }

    private int nextPos(int pos) {
        return (pos + 1) % arr.length;
    }

    @Override
    public T peek() {
        if (size == 0)
            return null;
        return arr[headPos];
    }

    @Override
    public T poll() {
        if (size == 0)
            return null;
        size--;
        T res = arr[headPos];
        headPos = nextPos(headPos);
        return res;
    }

    @Override
    public Iterator<T> iterator() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return 0;
    }

    public static void main(String[] args) {
        MyQueue<Integer> realQueue = new MyQueue<Integer>(Integer[].class, 10);
        System.out.println(realQueue.offer(20));
    }
    public MyQueue(Class<T[]> clazz, int length) {
        arr = clazz.cast(Array.newInstance(clazz.getComponentType(), length));
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

Thts probably because you hv not instantiated your arr[] array. Instantiate it somewhere in the constructor or do it in offer(T e) before the if(size < arr.length).
@MarshallTigerus I have updated the above code to add a constructor in which the array is instantiated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.