需要有关未经检查的操作的帮助 java

Need help on unchecked operations java

我自己在学习算法,我尝试从头开始用泛型在Java中实现LinkedList。我有一个带有 Object 的版本,效果很好,但是当我用泛型更新它时,它会发出警告。谁能帮忙 "unchecked or unsafe operations" 来自哪里?

class LinkedListGeneric <T> {
    private Node<T> head; 
    private int size;

    public LinkedListGeneric() {
       head = null;
       size = 0; 
    }

    public int size() {
        return size;
    }

    public void add (T data) {
        if (head == null) {
            head = new Node<T> (data);
            size = 1;
        }
        else {
            Node<T> temp = new Node<T> (data);
            search(size).setNext(temp);
            size++;
        }
    }

    public void add (T data, int position) {
        if (position > size + 1 || position <= 0) {
            System.out.println ("error.");
            return;
        }
        Node<T> temp = new Node<T> (data);
        if (position == 1) {
            temp.setNext(head);
            head = temp;
            return;
        }
        Node<T> prev = search(position - 1);
        temp.setNext(prev.getNext());
        prev.setNext(temp);
    }

    public void delete (int position) {
        if (position > size || position <= 0) {
            System.out.println ("error.");
            return;
        }
        if (position == 1) {
            size--;
            head = head.getNext();
            return;
        }
        Node<T> prev = search(position - 1);
        prev.setNext(prev.getNext().getNext());
        size--;
    }

    public T getValue (int position) {
        if (position > size || position <= 0) {
            System.out.println ("error.");
            return null;
        }
        Node<T> temp = search(position);
        return temp.getData();
        //return search(position).getData();
    }

    public int searchData(T data) {
        Node<T> temp = head;
        int position = 1;
        boolean flag = false;
        while (temp != null) {
            if (temp.getData() == data) {
                flag = true;
                break;
            }
            else {
                temp = temp.getNext();
                position++;
            }
        }
        if (flag) return position;
        else return -1;
    }

    public void print() {
        Node<T> temp = head;
        int position = 1;
        while (temp != null) {
            System.out.println("Node " + position + ": " + temp.getData());
            temp = temp.getNext();
            position++;
        }
    }

    private Node<T> search (int position) {
        Node temp = head;
        while (position > 0) {
            temp = temp.getNext();
        }
        return temp;
    }

    private class Node<T> {
        private T data;
        private Node<T> next;
        public Node() {
           this.data = null;
           next = null; 
        }

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

        public T getData() {
            return data;
        }

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

我看到的问题是您的 Node.getNext 调用返回 Node 而不是 Node<T>。这相当于返回 Node<Object> 而不是泛型类型的方法。

所以,你应该改变:

public Node getNext() {
    return next;
}

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

尽管 sbochin 的回答会修复您的一些警告,但执行以下操作会修复所有警告:

  1. Node class 中 T 的所有实例替换为 T2.[=37=,包括 class 声明中的实例]
  2. getNext的return改为Node<T2>
  3. setNext 中的参数类型更改为 Node<T2>
  4. searchtemp 的类型更改为 Node<T>
  5. 您可能还想将 @SuppressWarnings("unused") 添加到 public Node(),因为这也会生成编译器警告。

您可能还想使您的 Node class 成为静态 class 因为它的 none 方法取决于它所在的 LinkedListGeneric<T> 对象.

完全或者,您可以从 Node 中删除类型参数,这会删除除未使用警告之外的所有警告。但是,您必须保持 class 非静态。