简体   繁体   中英

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.

Problem Description. Everything was fine after i restricted the Parameter T in my Node Class. I had to, because T needed to implement the size-Method. It was necessary for the countAllElements() Method in Node-Class. 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> .

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:.

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 .

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

  • Remove the bound on the type parameter in the Node class, and supply a ToIntFunction<? super T> ToIntFunction<? super T> to the countAllElements method, to allow you to say "this is how you '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; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM