无法从节点< E>转换节点< E>? [英] Cannot convert from Node<E> to Node<E>?

查看:122
本文介绍了无法从节点< E>转换节点< E>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是从我的一本书中做了一些练习,我很好奇为什么我在eclipse中收到以下错误:

I am just doing some practice from one of my books, and I was curious about why I am getting the following error in eclipse:

类型不匹配:无法转换类型DoublyLinkedList.Node< E> to DoublyLinkedList.Node< E>

代码:

import java.util.Iterator;
    import java.util.ListIterator;
    import java.util.NoSuchElementException;


public class DoublyLinkedList<E extends Comparable<E>> implements Iterable<E>{

 private int size = 0;
 private Node<E> head;
 private Node<E> tail;

/** Returns a list iterator object for the list at 
 * the specified index
 */

 public DoublyLinkedList(){


}



private static class Node<E> {

    Node<E> next = null;
    Node<E> prev = null;
    E data;

    public Node(E dataItem){
        data = dataItem;
    }

    public Node(E dataItem, Node<E> previous, Node<E> nextNode){
        this(dataItem);
        prev = previous;
        next = nextNode;
    }

}


private class MyListIter<E> implements ListIterator<E>{

    private Node<E> lastReturned; // a link reference to the last item that was returned
    private Node<E> nextItem; // a link reference to the next item in the list
    /** The index of the current position */ 
    private int index = 0;

    public MyListIter(int pos){
        if (pos < 0 || pos > size)
            throw new IndexOutOfBoundsException("Invalid index: " + index);
        lastReturned = null;
        if (pos == size){
            index = size;
            nextItem = null;
        } else { // otherwise we will start at the beginning of the list, and loop until the position in the argument
            nextItem = head; // ERROR
            for (index = 0; index < pos; index++){
                nextItem = nextItem.next; // next item will always reference the list node that is called by the next method
            }

        }
    }

    @Override
    public void add(E element) {
        if (head == null){
            Node<E> newNode = new Node<E>(element);
            head = newNode; // ERROR
            tail = head;
        }


    }
    @Override
    public boolean hasNext() {
        return nextItem != null; // just checks to make sure there is a node following the current node
    }
    @Override
    public boolean hasPrevious() {
        return (nextItem == null && size != 0) || nextItem.prev != null;
    }
    @Override
    public E next() {
        if (!hasNext())
            throw new NoSuchElementException("There is no node at that location");
        lastReturned = nextItem;
        nextItem = nextItem.next;
        index++;
        return lastReturned.data;
    }
    @Override
    public int nextIndex() {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public E previous() {
        if (!hasPrevious())
            throw new NoSuchElementException();
        if (nextItem == null) // the iterator is at the end of the list
            nextItem = tail; // therefore, the nextItem is at the tail, so the previous is the tail. ERROR HERE TOO
        else
            nextItem = nextItem.prev;
        lastReturned = nextItem;
        index--;
        return lastReturned.data;
    }
    @Override
    public int previousIndex() {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public void remove() {
        // TODO Auto-generated method stub

    }
    @Override
    public void set(E arg0) {
        // TODO Auto-generated method stub

    }



}


@Override
public Iterator<E> iterator() {
    // TODO Auto-generated method stub
    return null;
}


}

我评论了哪里我正好在3个不同的位置得到错误。如果您能提供任何反馈,我会很感激。我的书没有解决它,我已经搜索过,似乎无法得到我正在寻找的答案。

I commented where exactly I am getting the error in 3 different locations. If you can provide any feedback, I'd appreciate it. My book doesn't address it and I have searched around and can't really seem to get the answer i'm looking for.

推荐答案

您已经声明了两种不同的泛型: E (对于节点)和 E扩展Comparable< E> (对于 DoublyLinkedList )。

You've declared two different generic types: E (for Node) and E extends Comparable<E> (for DoublyLinkedList).

这里的主要问题可能是 MyListIter ,这是一个非静态的内部类,因此自动继承 DoublyLinkedList E 的定义。因为它继承了 E 的定义,所以你应该将其声明为

The main issue here is probably MyListIter, which is a non-static inner class and as such automatically inherits DoublyLinkedList's definition of E. Because it inherits the definition of E, you should just be declaring it as

private class MyListIter implements ListIterator<E>

但你已经做到了 MyListIter< E> ,它将 E 重新定义为与 E 不同的 DoublyLinkedList users(隐式 E extends Object vs. E extends Comparable< E> )。

but you've made it MyListIter<E>, which is redefining E to something different than the E that DoublyLinkedList users (implicit E extends Object vs. E extends Comparable<E>).

认为 节点应该按原样工作,因为它是一个嵌套类(带 static keyword)并且不从 DoublyLinkedList E 的定义>。但是,将它声明为 DoublyLinkedList 的非静态内部类可能是有意义的(私有类Node )与 MyListIter 相同。

I think Node should work as-is since it is a nested class (with the static keyword) and doesn't inherit the definition of E from DoublyLinkedList. However, it'd probably make sense here to declare it as a non-static inner class of DoublyLinkedList (private class Node) the same as MyListIter.

此外,您应该允许 E 是某种类型的子类型类型,通过将其声明为 E来实现 Comparable 扩展可比较<?超级E>

Also, you should probably allow E to be a type that is a subtype of some type that implements Comparable by declaring it as E extends Comparable<? super E>.

这篇关于无法从节点&lt; E&gt;转换节点&lt; E&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆