简体   繁体   English

类型 T 不是有界参数的有效替代品 ` <t extends collection<?> &gt;</t>

[英]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();
    }

}

First above is my 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;
    }

}

Second Class is my Node-Class.第二个 Class 是我的节点级。

Problem Description.问题描述。 Everything was fine after i restricted the Parameter T in my Node Class.在我限制节点 Class 中的参数T后,一切都很好。 I had to, because T needed to implement the size-Method.我不得不这样做,因为 T 需要实现 size-Method。 It was necessary for the countAllElements() Method in Node-Class. Node-Class 中的countAllElements()方法是必需的。 In my List Class i get the error message: "Type T is not a valide Substitute for the bounded Parameter <T extends Collection<?>> of the type Node<T> . The error message appears everywhere where i use an instance of my object from the type Node<T> .在我的列表 Class 中,我收到错误消息:“类型 T 不是Node<T>类型的有界参数<T extends Collection<?>>的有效替代品。错误消息出现在我使用我的实例的任何地方object 来自Node<T>类型。

I hope i did everything Right in this Question by Posting my Code here.我希望通过在此处发布我的代码,我在这个问题中做的一切都是正确的。 Sorry for my case-shift, i live in Germany.对不起,我的案件转移,我住在德国。 I dont know what my Computer does D:.我不知道我的电脑做了什么 D:.

Edited: Sorry guys, i forgot to Change the title.编辑:对不起,我忘了更改标题。 I adjusted it.我调整了它。

As it stands, you are contradicting yourself: you are saying that your Node s can contain any T in your List class, but your Node class says they can contain any Collection .就目前而言,您自相矛盾:您是说您的Node s 可以包含List class 中的任何T ,但是您的Node class 说它们可以包含任何Collection

So, you either need to:因此,您要么需要:

  • Go through all of the Node<T> s in the List class, replacing them with something list Node<Collection<T>> , Node<List<T>> etc Go 通过List class 中的所有Node<T> ,将它们替换为列表Node<Collection<T>>Node<List<T>>

  • Remove the bound on the type parameter in the Node class, and supply a ToIntFunction<? super T>移除Node class 中类型参数的绑定,并提供一个ToIntFunction<? super T> ToIntFunction<? super T> to the countAllElements method, to allow you to say "this is how you 'count' a T ": ToIntFunction<? super T>countAllElements方法,让你说“这就是你‘数’一个T的方式”:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 绑定不匹配:MyClass1类型不是绑定参数的有效替代品 <T extends Comparator<T> &gt;类型的人 <T> - Bound mismatch: The type MyClass1 is not a valid substitute for the bounded parameter <T extends Comparator<T>> of the type Person<T> 边界不匹配:类型A不是边界参数的有效替代品 <T extends Entity> 类型为TestService <T> - Bound mismatch: The type A is not a valid substitute for the bounded parameter <T extends Entity> of the type TestService<T> 为什么“类型绑定不匹配:类型? 扩展T不是有界参数的有效替代品 <E extends Enum<E> &gt;枚举类型 <E> “? - Why ”Type Bound mismatch: The type ? extends T is not a valid substitute for the bounded parameter <E extends Enum<E>> of the type Enum<E>“? 有界类型参数(T 扩展)和上限通配符(?扩展)之间的区别 - Difference between Bounded Type parameter (T extends) and Upper Bound Wildcard (? extends) 有界不匹配:类型不是有界参数的有效替代品<e extends comparable<e> &gt; 类型</e> - Bound mismatch: The type is not a valid substitute for the bounded parameter <E extends Comparable<E>> of the type Java Generics - 绑定不匹配:类型 Object 不是绑定参数的有效替代品<e extends comparable<e> &gt;</e> - Java Generics - Bound mismatch: The type Object is not a valid substitute for the bounded parameter <E extends Comparable<E>> Java泛型:有界类型参数中的多重继承<T extends A & I> - Java Generics: Multiple Inheritance in Bounded Type Parameters <T extends A & I> “推断出的类型______不是边界参数的有效替代品” - “The inferred type _____ is not a valid substitute for the bounded parameter” 绑定不匹配:该类型不是绑定参数的有效替代 - Bound mismatch: The type is not a valid substitute for the bounded parameter Eclipse中有限的不匹配。 “该类型不是有界参数的有效替代” - Bounded mismatch in Eclipse. “The type is not a valid substitute for the bounded parameter”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM