java - LinkedList.java 使用未经检查或不安全的操作。注: Recompile with -Xlint:unchecked for details

标签 java linked-list

LinkedList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

当我编译 LinkedList 类时,我收到此消息。我假设这与我错误地使用泛型有关,但我不太确定我做错了什么。

这是 Node 类和 Linked List 类的代码。


Node.java

public class Node<T> {

    private T data;
    private Node<T> next;

    public Node() {
        this.data = null;
        this.next = null;
    }

    public Node(T data, Node<T> next) {
        this.data = data;
        this.next = next;
    }

    public void setData(T data) {
        this.data = data;
    }

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

    public T getData() {
        return this.data;
    }

    public Node getNext() {
        return this.next;
    }

}

LinkedList.java

public class LinkedList<T> {

    private Node<T> head;

    public LinkedList() {
        this.head = null;
    }

    public LinkedList(Node<T> head) {
        this.head = head;
    }

    public void add(T data) {
        if(this.isEmpty())
            this.head = new Node<>(data, null);

        else {
            Node<T> current = this.head;
            while(current.getNext() != null)
                current = current.getNext();
            current.setNext(new Node<>(data, null));
        }
    }

    public T remove() {
        Node<T> current = this.head;
        Node<T> follow = null;

        while(current.getNext() != null) {
            follow = current;
            current = current.getNext();
        }

        if(follow == null)
            this.head = null;
        else
            follow.setNext(null);

        return current.getData();
    }

    public int size() {
        Node current = this.head;
        int count = 0;
        while(current != null) {
            count++;
            current = current.getNext();
        }
        return count;
    }

    public boolean contains(T data) {
        boolean result = false;

        Node current = this.head;
        while(current != null) {
            if(current.getData() == data)
                result = true;
            current = current.getNext();
        }

        return result;
    }

    public boolean isEmpty() {
        return (this.head == null);
    }

    public String toString() {
        if(this.isEmpty())
            return "[]";

        String output = "[";
        Node<T> current = this.head;
        while(current != null) {
            output += current.getData();
            if(current.getNext() != null)
                output += ", ";
            current = current.getNext();
        }
        output += "]";
        return output;
    }

}

最佳答案

此代码会导致编译警告:

public Node getNext() {
    return this.next;
}

当您执行以下操作时:

current = current.getNext();

current声明为:

Node<T> current;

getNext应该是:

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

以及所有出现 Node 的情况应该是Node<T> .

关于java - LinkedList.java 使用未经检查或不安全的操作。注: Recompile with -Xlint:unchecked for details,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27478437/

相关文章:

c - C 中的链接列表不打印最后一个值

c - 以正确的顺序打印链表

java - 如何限制 Eclipse 中的搜索匹配以过滤掉评论中的结果?

java - 为什么数组的最小值总是0?

java - 如何修复链接列表中的插入方法,以便第一个元素不会插入两次?

c - 编写一个返回列表中字符串位置的函数(C 代码)

c++ - 如何避免出现分段核心转储错误?

java - Java 自定义图形的最佳方法

java - 无法解析项目 org.openjdk.jmh :jmh-core:jar:1. 21 的依赖项

java - 如何检查我们在 jdbc 中使用的是 oracle 8i 数据库?