类型 T 不是有界参数 `<T extends Collection<?>> 的有效替代

Type T is not a valide Substitute for the bounded Parameter `<T extends Collection<?>>

package einfuehrung.knodenUndListeKopie;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class List<T> {

    private class ListIterator<K> implements Iterator<T> {
        private Node<T> node = null;

        public ListIterator() {
            node = head;
        }

        @Override
        public boolean hasNext() {
            return node.getNext() != null;
        }

        @Override
        public T next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            node = node.getNext();
            T obj = node.getObject();
            return obj;
        }

    }

    public Iterator<T> iterator() {
        ListIterator<T> iter = new ListIterator<T>();
        return iter;
    }

    private Node<T> head;

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

    public Node<T> getHead() {
        return head;
    }

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

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

    public void addFirst(T element) {
        Node<T> node = new Node<T>();
        Node<T> nextNode = head.getNext();
        node.setObject(element);
        node.setNext(nextNode);
        head.setNext(node);

    }

    public void addLast(T element) {
        Node<T> node = new Node<T>();
        Node<T> lastNode = head;

        while (lastNode.getNext() != null) {
            lastNode = lastNode.getNext();
        }

        lastNode.setNext(node);
        node.setNext(null);
        node.setObject(element);
    }

    public Object removeFirst() {
        Object solution;
        if (isEmpty()) {
            solution = null;
        }
        Node<T> node = head.getNext();
        Node<T> nextNode = node.getNext();
        solution = node.getObject();
        head.setNext(nextNode);

        return solution;
    }

    public Object removeLast() {
        Object solution;
        if (isEmpty()) {
            solution = null;
        }

        Node<T> beforeLastNode = head;
        Node<T> lastNode;

        while (beforeLastNode.getNext().getNext() != null) {
            beforeLastNode = beforeLastNode.getNext();
        }

        lastNode = beforeLastNode.getNext();
        solution = lastNode.getObject();
        beforeLastNode.setNext(null);

        return solution;
    }

    /**
     * It does not delete the node, where the element is saved.
     * 
     * @return first element of list
     */
    public Object getFirstElement() {

        return head.getNext().getObject();
    }

}

上面第一个是我的List-Class.

    package einfuehrung.knodenUndListeKopie;

import java.util.Collection;

public class Node<T extends Collection<?>> {

    private Node<T> next;
    private T object;

    public Node() {

    }

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

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

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

    public T getObject() {
        return object;
    }

    public void setObject(T object) {
        this.object = object;
    }
    
    public int countAllElements() {
        int solution;
        solution = object.size();
        if (this.next != null) {
            solution += this.next.countAllElements();
        }
        
        
        
        return solution;
    }

}

第二个Class是我的Node-Class。

问题描述。在我限制节点 Class 中的参数 T 后,一切都很好。我不得不这样做,因为 T 需要实现 size-Method。 Node-Class 中的 countAllElements() 方法是必需的。在我的列表 Class 中,我收到错误消息:“类型 T 不是类型 Node<T> 的有界参数 <T extends Collection<?>> 的有效替代品。错误消息出现在我使用实例的任何地方我的 object 来自类型 Node<T>

我希望通过在此处发布我的代码,我在这个问题上做的一切都是正确的。对不起我的 case-shift,我住在德国。我不知道我的电脑在做什么 D:.

已编辑:对不起大家,我忘了更改标题。我调整了。

就目前而言,您是在自相矛盾:您是说您的 Node 可以在 List class 中包含任何 T,但是您的 Node class 表示它们可以包含任何 Collection.

因此,您需要:

  • 遍历 List class 中的所有 Node<T>,用列表 Node<Collection<T>>Node<List<T>> 替换它们等等

  • 删除 Node class 中类型参数的绑定,并向 countAllElements 方法提供 ToIntFunction<? super T>,以允许您说“这就是你 'count' a T 的方式”:

    public int countAllElements(ToIntFunction<? super T> counter) {
      int solution = counter.apply(object);
      if (this.next != null) {
        solution += this.next.countAllElements(counter);
      }   
      return solution;
    }