【问题标题】:Type T is not a valide Substitute for the bounded Parameter `<T extends Collection<?>>类型 T 不是有界参数 `<T extends Collection<?>> 的有效替代品
【发布时间】:2020-08-05 08:33:32
【问题描述】:
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;
    }

}

第二类是我的节点类。

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

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

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

【问题讨论】:

  • 您想知道Collections 类和Collection 接口之间的区别,还是您对泛型有一些问题?我不知道你在问什么

标签: java list collections nodes implementation


【解决方案1】:

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

所以,您要么需要:

  • 遍历List 类中的所有Node&lt;T&gt;s,将它们替换为Node&lt;Collection&lt;T&gt;&gt;Node&lt;List&lt;T&gt;&gt; 等列表

  • 删除Node 类中类型参数的绑定,并为countAllElements 方法提供ToIntFunction&lt;? super T&gt;,以允许您说“这就是您'计数'T 的方式”:

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

【讨论】:

  • 谢谢你,先生!!!。我现在意识到我的问题。我将使用第一个建议。我不明白第二个功能。也许我必须在 API 中查找。你为什么不直接写: public int countAllElements(ToIntFunction counter) ?这会是个问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-22
  • 2020-07-20
  • 1970-01-01
相关资源
最近更新 更多