加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 是否不可能有一个基于数组的队列与泛型?

发布时间:2020-12-15 04:29:37 所属栏目:Java 来源:网络整理
导读:参见英文答案 How to create a generic array in Java?????????????????????????????????????29个 这是一个基于数组的队列,对于int: /** * Array based * @author X220 * */public class MyQueue { private int[] _data; private int MAX_SIZE; private int
参见英文答案 > How to create a generic array in Java?????????????????????????????????????29个
这是一个基于数组的队列,对于int:

/**
 * Array based
 * @author X220
 *
 */

public class MyQueue {

    private int[] _data;
    private int MAX_SIZE;
    private int front = -1;
    private int back = 0;
    private int elementsCount = 0;

    public void printQueue()
    {
        int j = this.front + 1;
        int i = 0;
        while (i < this._data.length && i < elementsCount)
        {
            System.out.println("At location " + j % MAX_SIZE + " element :" + this._data[j % MAX_SIZE]);
            j++;
            i++;
        }
    }

    public MyQueue(int _size)
    {
        MAX_SIZE = _size > 0 ? _size : 10;
        this._data = new int[MAX_SIZE];
    }

    public boolean IsEmpty()
    {
        return this.elementsCount == 0;     
    }

    public boolean IsFull()
    {
        return this.elementsCount == MAX_SIZE;
    }

    public void Push(int pushMe) throws QueueIsFullException
    {
        if (IsFull())
        {
            throw new QueueIsFullException("Queue is full");
        }
        this.elementsCount++;
        _data[back++ % MAX_SIZE] = pushMe;
    }

    public int Pop() throws QueueIsEmptyException 
    {
        if (IsEmpty())
        {
            throw new QueueIsEmptyException("Queue is full");
        }
        elementsCount--;
        return _data[++front % MAX_SIZE];
    }

    public static void main(String args[])
    {
        try
        {
            MyQueue q1 = new MyQueue(15);
            q1.Push(1);
            q1.Push(2);
            q1.Push(3);
            q1.Push(4);
            q1.Push(5);
            q1.Pop();
            q1.Pop();
            q1.Pop();
            q1.Pop();
            q1.Pop();
            q1.Push(6);
            q1.Pop();
            q1.Push(7);
            q1.Push(8);
            q1.Push(9);
            q1.Push(10);
            q1.Push(11);
            q1.Push(12);


//          q1.Push(1);
//          q1.Push(2);
//          q1.Push(3);
//          q1.Push(4);
//          q1.Push(5);
//          q1.Push(7);
//          q1.Push(8);
//          q1.Push(9);
//          q1.Push(10);
//          q1.Push(11);
//          q1.Push(12);
//          q1.Push(40);
//          q1.Push(50);
            q1.printQueue();

        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }


    @SuppressWarnings("serial")
    class QueueIsFullException extends Exception 
    {
          public QueueIsFullException(String message){
             super(message);
          }
    }

    @SuppressWarnings("serial")
    class QueueIsEmptyException extends Exception 
    {
          public QueueIsEmptyException(String message){
             super(message);
          }

    }

}

我想使用泛型,所以我将int更改为T但是我得到了这个:

public class MyQueue <T>{

    private T[] _data;
    private int MAX_SIZE;
    private int front = -1;
    private int back = 0;
    private int elementsCount = 0;

    public void printQueue()
    {
        int j = this.front + 1;
        int i = 0;
        while (i < this._data.length && i < elementsCount)
        {
            System.out.println("At location " + j % MAX_SIZE + " element :" + this._data[j % MAX_SIZE]);
            j++;
            i++;
        }
    }

    public MyQueue(int _size)
    {
        MAX_SIZE = _size > 0 ? _size : 10;
        this._data = new T[MAX_SIZE];
    }

....


}

那个:

  • Cannot create a generic array of T

从this post的答案我看到我不能将泛型用于数组.

这是否意味着基于数组的泛型队列没有解决方法?我必须切换到其他一些数据结构吗?

解决方法

您的问题的根本原因不在于您的MyQueue类,我认为您误解了Java处理泛型的方式.通用类型仅在编译时存在,之后它们只是从字节代码中删除,并且在运行时只有真正的Java类型存在于幕后.

这就是您无法实例化泛型类型的原因,因为在运行时此参数化类型根本不存在.

你可以做的是提供一个真实的类(扩展T)作为MyQueue类中的参数实例化这个类类型,因为这是一个一流的Java类型.

这是一个非常相似的StackOverflow问题和解决方案:
Instantiating a generic class in Java

还建议阅读关于泛型的Java参考,就像你原来问题的答案在这里:
https://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createObjects

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读