LinkedList.java使用未经检查或不安全的操作。注意:使用-Xlint重新编译:取消选中以获取详细信息

时间:2014-12-15 06:23:18

标签: java linked-list

  

LinkedList.java使用未经检查或不安全的操作。   注意:使用-Xlint重新编译:取消选中以获取详细信息。

我在编译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;
    }

}

2 个答案:

答案 0 :(得分:1)

在某些地方您使用原始Node类型(size()contains()方法):

Node current = this.head;

将其更改为

Node<T> current = this.head;

Node getNext()也应更改为Node<T> getNext()

答案 1 :(得分:1)

此代码会导致编译警告:

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

执行以下操作时:

current = current.getNext();

current声明为:

Node<T> current;

getNext应该是:

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

Node的所有出现都应为Node<T>