Type T is not a valid substitute for the bounded parameter `>`.

huangapple go评论76阅读模式
英文:

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类的翻译。

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

}

第二个代码块是您提供的Node类的翻译。

请注意,我已经删除了一些与问题描述和标题不相关的内容。如果您需要更多帮助或其他翻译,请随时提问。

英文:
package einfuehrung.knodenUndListeKopie;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class List&lt;T&gt; {
private class ListIterator&lt;K&gt; implements Iterator&lt;T&gt; {
private Node&lt;T&gt; 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&lt;T&gt; iterator() {
ListIterator&lt;T&gt; iter = new ListIterator&lt;T&gt;();
return iter;
}
private Node&lt;T&gt; head;
public List() {
this.head = new Node&lt;T&gt;();
}
public Node&lt;T&gt; getHead() {
return head;
}
public void setHead(Node&lt;T&gt; head) {
this.head = head;
}
public boolean isEmpty() {
return head.getNext() == null;
}
public void addFirst(T element) {
Node&lt;T&gt; node = new Node&lt;T&gt;();
Node&lt;T&gt; nextNode = head.getNext();
node.setObject(element);
node.setNext(nextNode);
head.setNext(node);
}
public void addLast(T element) {
Node&lt;T&gt; node = new Node&lt;T&gt;();
Node&lt;T&gt; 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&lt;T&gt; node = head.getNext();
Node&lt;T&gt; nextNode = node.getNext();
solution = node.getObject();
head.setNext(nextNode);
return solution;
}
public Object removeLast() {
Object solution;
if (isEmpty()) {
solution = null;
}
Node&lt;T&gt; beforeLastNode = head;
Node&lt;T&gt; 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&lt;T extends Collection&lt;?&gt;&gt; {
private Node&lt;T&gt; next;
private T object;
public Node() {
}
public Node(Node&lt;T&gt; next, T object) {
this.next = next;
this.object = object;
}
public Node&lt;T&gt; getNext() {
return next;
}
public void setNext(Node&lt;T&gt; 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 &lt;T extends Collection&lt;?&gt;&gt; of the type Node&lt;T&gt;. The error message appears everywhere where i use an instance of my object from the type Node&lt;T&gt;.

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.

答案1

得分: 0

你目前存在自相矛盾:你说你的 Node 可以包含 List 类中的任何 T,但你的 Node 类表明它们可以包含任何 Collection

所以,你可以选择:

  • 遍历 List 类中所有的 Node&lt;T&gt;,将它们替换为类似 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&lt;? super T&gt; counter) {
    int solution = counter.apply(object);
    if (this.next != null) {
        solution += this.next.countAllElements(counter);
    }   
    return solution;
}
英文:

As it stands, you are contradicting yourself: you are saying that your Nodes 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&lt;T&gt;s in the List class, replacing them with something list Node&lt;Collection&lt;T&gt;&gt;, Node&lt;List&lt;T&gt;&gt; etc

  • Remove the bound on the type parameter in the Node class, and supply a ToIntFunction&lt;? super T&gt; to the countAllElements method, to allow you to say "this is how you 'count' a T":

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

huangapple
  • 本文由 发表于 2020年8月5日 16:33:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63261317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定