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

标签 java generics

我自己正在学习算法,我尝试从头开始在 Java 中使用泛型类型实现 LinkedList。我有一个带有 Object 的版本,运行良好,但是当我使用泛型类型更新它时,它会发出警告。谁能帮忙看看“未经检查或不安全的操作”从何而来?

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;
}

关于java - 需要有关未经检查的操作java的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28228530/

相关文章:

java - 安卓sql数据库

java - 使用 PhotoView 放大后无法达到图像的原始大小

Java 重构为泛型行业标准

c# - 通用子 C# 的通用父级

java - Jackson如何将json反序列化为泛型?

Scala:使用泛型类型参数声明方法

java - SimpleDateFormat 如何依赖于语言环境?

java - Spring中的ObjectFactory和FactoryBean接口(interface)有什么不同

java SAXParser 忽略异常并继续解析

java - 为什么泛型方法和泛型类型有不同的类型引入语法?