java - 在 Java 中扩展非常基本的 Node<T> 的基本链表类型队列 + 请求外行术语

标签 java nodes

首先,是的,这是学校的作业,但我并不是要找人以任何方式重写或修改我的代码。我的问题是:

我被要求编写一个类来创建一个扩展 Node 的队列(后者如下所示)

public class Node<T>{
  protected      T  data;
  protected Node<T> next; 
}

我已经编写了(很可能是非常粗糙的)方法来执行此操作,以及一个将整数类型存储到队列中的基本测试程序(希望如此)。我不知道所有的专业术语,我已经阅读了“泛型”文档,但可能错过了一个关键点,我已经阅读了链接列表的工作原理(他们的示例在节点类中有更多,这是我的东西' m 不允许在此作业中编辑),以及圆形阵列等。当我运行我的代码时,我收到了一个我没有预料到的关于类型的错误。我会发布我的相关代码,有人可以大致解释一下我做了什么来得到这个(而不是......我的代码中我不应该使用的地方?)

public class Queue<T> extends Node<T> {

    public Node base;
    public Node end;

    public void enqueue(T item) {

        Node newItem = new Node();
        newItem.data = item;
        newItem.next = null;

        if (isEmpty()) { //Sets to item count of 1
            this.base = newItem; //Base becomes the new item
            this.base.next = this.end; //Base Points Next as End
            this.end.next = this.base; //End points to the one before it (base)
        } 
        else { //Sets to item count above 1.
            this.end.next.next = newItem; //The Last Item Now Points Next as the New Item (End is never counted as an item)
            this.end.next = newItem; //End now points to the next final item.
        }

    }

    public T dequeue() {

        if (isEmpty()) {
            return (null);
        }

        else {
            T item = this.base.data;

            if (this.base.next == this.end) {
                this.base = null;
                this.end = null;
            }

            else {
                this.base = this.base.next;
            }

            return (item);
        }

    }

    public int size() {

        int count = 0;

        for (Node node = base; node != null; node = node.next) {
            count++;
        }
        return count;

    }

    public boolean isEmpty() {

        return (base == null);

    }

    public Queue() {

        this.base = null;
        this.end = null;

    }
 }

TestQueue.java 代码是:

public class TestQueue {

    public static void main(String args[]) {

        QueueStuff<Integer> firstQueue = new QueueStuff<>();

        firstQueue.enqueue (66);
        firstQueue.enqueue (6);
        firstQueue.enqueue (666);
        firstQueue.enqueue (0);
        firstQueue.enqueue (6666);

        //firstQueue.printQueue();
    }

}

错误是这样的:

incompatible types. 
   T item = this.base.data;
                     ^
   required: T
   found:    Object
   where T is a Type variable: T extends Object declared in class Queue

最佳答案

这里:

public Node base;
public Node end;

应该是:

public Node<T> base;
public Node<T> end;

问题是:泛型都是关于编译时的类型检查。当你执行任务时:

T item = this.base.data; 

编译器不允许,因为它不进行类型检查。它不进行类型检查,因为:

public Node base; 

相当于:

public Node<Object> base; 

关于java - 在 Java 中扩展非常基本的 Node<T> 的基本链表类型队列 + 请求外行术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15281927/

相关文章:

nodes - Gephi未在图形上显示“节点和边”

c++ - 如何将链表的节点直接链接到节点指针?

java - 将节点添加到节点列表

java - 将 google maps v2 添加到 Fragment **更新正确答案**

java - Selenium Webdriver 和 Firefox Server Not Found 错误

java - ActionListener 和 ActionEvent 位于不同的类中

java - 当所有div类名和span类名相同时如何使用java中的jsoup找到内部元素

java - 如果按钮被读取为节点,如何在 JavaFX 中获取按钮的文本?循环遍历按钮组/VBox。将其作为节点返回

java - 使用 Spring Email 发送电子邮件

python - Networkx 可以从不同文件中读取节点和边吗?