【发布时间】:2020-08-05 08:33:32
【问题描述】:
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-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;
}
}
第二类是我的节点类。
问题描述。在我的节点类中限制参数T 后一切都很好。我不得不这样做,因为 T 需要实现 size-Method。对于 Node-Class 中的 countAllElements() 方法,它是必需的。在我的列表类中,我收到错误消息:“类型 T 不是 Node<T> 类型的有界参数 <T extends Collection<?>> 的有效替代品。错误消息出现在我使用 @987654327 类型的对象实例的任何地方@。
我希望通过在此处发布我的代码,我在这个问题中做的一切都是正确的。对不起,我的案件转移,我住在德国。我不知道我的电脑做了什么 D:.
已编辑:对不起,我忘了更改标题。我调整了。
【问题讨论】:
-
您想知道
Collections类和Collection接口之间的区别,还是您对泛型有一些问题?我不知道你在问什么
标签: java list collections nodes implementation