Java 中的 LinkedList 实现,具有泛型和增强功能
我需要您检查一下我对单链表 (SLL) 的实现。实现应该使用泛型并能够使用增强的 for。
问题是,当我将 for (Number n : list)
作为 list
时,MyLinkedList
或 MyLinkedList
,我收到错误:“类型不匹配:无法从元素类型对象转换为数字”。
这就是我所拥有的。我不太确定的部分是泛型和迭代器。
提前致谢。
import java.util.Iterator;
public class MyLinkedList<T> implements Iterable<Object>
{
private Node head;
public MyLinkedList ()
{
head = null;
}
public void add (Node n)
{
if (head == null)
{
head = n;
}
else
{
Node node = head;
while (node.next != null)
{
node = node.next;
}
node = n;
}
}
public Iterator iterator()
{
return new MyLinkedListIterator (head);
}
public int size ()
{
int ret = 0;
MyLinkedListIterator it = new MyLinkedListIterator (head);
while (it.hasNext ())
{
it.next();
ret++;
}
return ret;
}
public Node getHead ()
{
return head;
}
}
class MyLinkedListIterator<T> implements Iterator
{
private Node node;
public MyLinkedListIterator (Node h)
{
node = h;
}
public MyLinkedListIterator (MyLinkedList<T> l)
{
this(l.getHead ());
}
public boolean hasNext ()
{
if (node.next == null)
{
return false;
}
else
{
return true;
}
}
public Object next ()
{
return node.next;
}
public void remove ()
{
}
}
I need you to review my implementation of a Singly Linked List (SLL) please. The implementation should use generics and be able to use the enhanced for.
The problem is that, when I do for (Number n : list)
being list
a MyLinkedList<Integer>
or MyLinkedList<Double>
, I get the error: "Type mismatch: cannot convert from element type Object to Number".
This is what I have. The parts I am not very certain about are the generics and the iterators.
Thanks in advance.
import java.util.Iterator;
public class MyLinkedList<T> implements Iterable<Object>
{
private Node head;
public MyLinkedList ()
{
head = null;
}
public void add (Node n)
{
if (head == null)
{
head = n;
}
else
{
Node node = head;
while (node.next != null)
{
node = node.next;
}
node = n;
}
}
public Iterator iterator()
{
return new MyLinkedListIterator (head);
}
public int size ()
{
int ret = 0;
MyLinkedListIterator it = new MyLinkedListIterator (head);
while (it.hasNext ())
{
it.next();
ret++;
}
return ret;
}
public Node getHead ()
{
return head;
}
}
class MyLinkedListIterator<T> implements Iterator
{
private Node node;
public MyLinkedListIterator (Node h)
{
node = h;
}
public MyLinkedListIterator (MyLinkedList<T> l)
{
this(l.getHead ());
}
public boolean hasNext ()
{
if (node.next == null)
{
return false;
}
else
{
return true;
}
}
public Object next ()
{
return node.next;
}
public void remove ()
{
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Iterable
而不是Iterable
add(Node)
实际上并不将对象添加到列表中。MyLinkedListIterator
应实现Iterator
。MyLinkedListIterator.hasNext()
将抛出NullPointerException
。MyLinkedListIterator.next()
不会移动到列表中的下一项。Iterable<T>
instead ofIterable<Object>
.add(Node)
doesn't actually add an object to the list.MyLinkedListIterator<T>
should implementIterator<T>
.MyLinkedListIterator.hasNext()
will throw aNullPointerException
if the list is empty.MyLinkedListIterator.next()
doesn't move to the next item in the list.您应该从
iterator
方法返回一个Iterator
,并且还应该扩展Iterable
而不是Iterable
此外,您的
MyLinkedListIterator
应该实现Iterator
。那么它应该可以工作。You should return an
Iterator<T>
from theiterator
method and you should also extendIterable<T>
instead ofIterable<Object>
.Besides, your
MyLinkedListIterator<T>
should implementIterator<T>
. Then it should work.除了其他人所说的之外,您可能不应该在公共方法中公开 Node - 节点应该是实现的纯粹内部方面。
On top of what the others have said, you probably shouldn't be exposing
Node
in your public methods - nodes should be a purely internal aspect of the implementation.为什么不使用
看这里< /a> 全面实施
Why dont you use
<E>
Look here for a comprehensive implementation
扩展要点:MyLinkedListIterator.next() 不会移动到列表中的下一项。
下一个方法应该是这样的,以使其正常工作:
Expanding the point: MyLinkedListIterator.next() doesn't move to the next item in the list.
the next method should be something along these lines to get it working: