[英]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.