带有泛型的 Java 中的 LinkedList 实现并增强了

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3799876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:26:07  来源:igfitidea点击:

LinkedList implementation in Java with generics and enhanced for

javagenericsdata-structuresiteratoriterable

提问by nunos

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.

我需要你检查我对单链表 (SLL) 的实现。实现应该使用泛型并且能够使用增强的 for。

The problem is that, when I do for (Number n : list)being lista MyLinkedList<Integer>or MyLinkedList<Double>, I get the error: "Type mismatch: cannot convert from element type Object to Number".

问题是,当我for (Number n : list)list一个MyLinkedList<Integer>或者MyLinkedList<Double>,我得到的错误:“类型不匹配:不能从元素类型的对象转换为数字”。

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 () 
    {

    }   
}

回答by Steve Emmerson

  • You should have Iterable<T>instead of Iterable<Object>.
  • add(Node)doesn't actually add an object to the list.
  • MyLinkedListIterator<T>should implement Iterator<T>.
  • MyLinkedListIterator.hasNext()will throw a NullPointerExceptionif the list is empty.
  • MyLinkedListIterator.next()doesn't move to the next item in the list.
  • 你应该有Iterable<T>而不是Iterable<Object>.
  • add(Node)实际上并没有将对象添加到列表中。
  • MyLinkedListIterator<T>应该执行Iterator<T>
  • MyLinkedListIterator.hasNext()NullPointerException如果列表为空,将抛出一个。
  • MyLinkedListIterator.next()不会移动到列表中的下一项。

回答by Ronald Wildenberg

You should return an Iterator<T>from the iteratormethod and you should also extend Iterable<T>instead of Iterable<Object>.

您应该Iterator<T>iterator方法中返回 an并且您还应该扩展Iterable<T>而不是Iterable<Object>

Besides, your MyLinkedListIterator<T>should implement Iterator<T>. Then it should work.

此外,您MyLinkedListIterator<T>应该实施Iterator<T>. 那么它应该工作。

回答by bragboy

Why dont you use <E>

你为什么不使用 <E>

public class Node<E>{
 E data;
 Node<E> next;
}

public class SinglyLinkedList<E> {

 Node<E> start;
 int size;
 .......
}

Look herefor a comprehensive implementation

看看这里关于全面执行

回答by Andy

On top of what the others have said, you probably shouldn't be exposing Nodein your public methods - nodes should be a purely internal aspect of the implementation.

除了其他人所说的之外,您可能不应该Node在公共方法中公开 - 节点应该是实现的纯粹内部方面。

回答by Raghavan

Expanding the point: MyLinkedListIterator.next() doesn't move to the next item in the list.

扩展一点: MyLinkedListIterator.next() 不会移动到列表中的下一项。

the next method should be something along these lines to get it working:

下一个方法应该是沿着这些路线的东西,以使其工作:

public T next() {
    if(isFirstNode) {
        isFirstNode = false;
        return node.data;
    }
    node = node.next;
    return node.data;
}