当前位置: 首页 > 知识库问答 >
问题:

字符串类不能强制转换为节点类

盖晋
2023-03-14

我有一个家庭作业要重构一些代码来使用泛型。我觉得我为类做得很对。这是我的东西

public class Node<T> {

    private T content;
    private T next;

    public Node(T content, T next) {
        this.content = content;
        this.next = next;
    }

    public T getContent() {
        return content;
    }

    public void setContent(T content) {
        this.content = content;
    }

    public T getNext() { return next; }

    public void setNext(T next) {
        this.next = next;
    }
}
public class LinkedList<T> {
    private Node head;

    public LinkedList() {
        head = null;
    }

    public void addFirst(T content) {
        Node<Object> ptr = head;
        head = new Node<>(content,ptr);
    }

    public void addLast(T content) {
        Node<Object> last = new Node<>(content,null);
        if (head == null) {
            head = last;
        } else {
            Node<Node> ptr = new Node(content, head);
            while(ptr.getNext() != null) {
                ptr = ptr.getNext();
            }
            ptr.setNext(last);
        }
    }

当涉及到实例化时,我得到一个ClassCastException错误:

线程“main”java.lang.ClassCastException中出现异常:类java.lang.String无法强制转换为类edu.csc.node(java.lang.String位于加载程序“bootstrap”的模块java.base中;edu.csc.node位于加载程序“app”的未命名模块中)

下面是导致错误的主类中的内容。

public class Main {

    public static void main(String[] args) {
        list1();
        list2();
    }

    // Use linked list with Strings
    public static void list1() {
        LinkedList<String> llist = new LinkedList<String>();
        llist.addFirst("one");
        llist.addLast("two");
        llist.addLast("three");
        llist.addFirst("zero");
        llist.addLast("xxxx");
        llist.addFirst("yyyyy");
        llist.deleteLast();
        llist.deleteFirst();
        System.out.println(llist);
        llist.deleteFirst();
        llist.deleteLast();
        llist.deleteFirst();
        llist.deleteLast();
        System.out.println(llist);
    }

    // Use linked list with Integers
    public static void list2() {
        LinkedList<Integer> llist = new LinkedList<Integer>();
        llist.addFirst(1);
        llist.addLast(2);
        llist.addLast(3);
        llist.addFirst(0);
        llist.addLast(142);
        llist.addFirst(-97);
        llist.deleteLast();
        llist.deleteFirst();
        System.out.println(llist);
        llist.deleteFirst();
        llist.deleteLast();
        llist.deleteFirst();
        llist.deleteLast();
        System.out.println(llist);
    }
}

有人能指出是什么造成的吗?

共有1个答案

赫连华皓
2023-03-14

node中,next变量的类型错误:

public class Node<T> {

private T content;
private Node<T> next; // this is a reference to the next node

public Node(T content, Node<T> next) { // next is a Node<T>
    this.content = content;
    this.next = next;
}

...

public Node<T> getNext() { return next; }

public void setNext(Node<T> next) {
    this.next = next;
}

您的LinkedList还需要更经常地引用T:

public void addFirst(T content) {
    Node<T> ptr = head;
    head = new Node<T>(content, ptr);
}

当列表中已经有至少一个项目时,AddLast的逻辑似乎很奇怪。您应该找到列表中的最后一个项目,然后在它后面添加一个节点。

Node<T> lastItem = head;
while(lastItem.getNext() != null) {
    lastItem = lastItem.getNext();
}
lastItem.setNext(new Node(content, last););
 类似资料: