试图学习java中的泛型和编译器抛出不能在非静态上下文中引用这个变量,即使我没有实际调用它 /* *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。 *要更改此模板文件,请选择工具|模板 *然后在编辑器中打开模板。 */ 包类型; /** * *@author Capn_Clunks */ 公共类泛型 { public class Queue//Inbetween表示泛型类型 { 私有E[]元素;//[]表示数组类型 头,尾; @抑制警告(“未选中”) 队列(整数大小) { 如果(尺寸

试图学习java中的泛型和编译器抛出不能在非静态上下文中引用这个变量,即使我没有实际调用它 /* *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。 *要更改此模板文件,请选择工具|模板 *然后在编辑器中打开模板。 */ 包类型; /** * *@author Capn_Clunks */ 公共类泛型 { public class Queue//Inbetween表示泛型类型 { 私有E[]元素;//[]表示数组类型 头,尾; @抑制警告(“未选中”) 队列(整数大小) { 如果(尺寸,java,Java,直接从一本书中复制出来,所以应该可以工作,但我不明白为什么,因为我不能使其成为静态的,我只是想编译一个例子来演示这个概念。您的队列类是泛型的内部(非静态嵌套)类。将其移动到顶级类,最简单的方法是将Queue设置为顶级类,并完全删除GenericTypes /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose

直接从一本书中复制出来,所以应该可以工作,但我不明白为什么,因为我不能使其成为静态的,我只是想编译一个例子来演示这个概念。

您的
队列
类是
泛型的内部(非静态嵌套)类。将其移动到顶级类,最简单的方法是将
Queue
设置为顶级类,并完全删除
GenericTypes

/*
 * 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());
    }
}