实现队列,在使其成为通用时出错

时间:2013-05-30 01:02:26

标签: java generics object queue

我对这个AbstractQueue实现的错误感到困惑。

此代码导致错误:

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

Eclipse说我需要从MyQueue中删除泛型类型。

这是实施的课程:

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;
    }
}

这是我第一次实现队列,也是我第一次创建一个使用泛型的类,所以我完全不知道该怎么做。当我按照Eclipse的建议时,我得到一个空指针异常,当我留下通用代码时,它不会编译。

1 个答案:

答案 0 :(得分:1)

我认为你没有正确使用泛型,所以我修改了你的课程。请检查它是否适合您.....
UPDATE :我稍微更改了代码以实例化arr[]数组。在底部main方法中添加了constructor

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));
    }

}