英文:
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<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.
答案1
得分: 0
你目前存在自相矛盾:你说你的 Node
可以包含 List
类中的任何 T
,但你的 Node
类表明它们可以包含任何 Collection
。
所以,你可以选择:
- 遍历
List
类中所有的Node<T>
,将它们替换为类似Node<Collection<T>>
、Node<List<T>>
等。 - 在
Node
类中移除类型参数的限制,并向countAllElements
方法提供一个ToIntFunction<? super T>
,以允许你指定如何“计数”一个T
:
public int countAllElements(ToIntFunction<? super T> 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 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 theList
class, replacing them with something listNode<Collection<T>>
,Node<List<T>>
etc -
Remove the bound on the type parameter in the
Node
class, and supply aToIntFunction<? super T>
to thecountAllElements
method, to allow you to say "this is how you 'count' aT
":public int countAllElements(ToIntFunction<? super T> counter) { int solution = counter.apply(object); if (this.next != null) { solution += this.next.countAllElements(counter); } return solution; }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论