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:
`Type mismatch: cannot convert from type DoublyLinkedList.Node to DoublyLinkedList.Node`
Code:
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
public class DoublyLinkedList> implements Iterable{
private int size = 0;
private Node head;
private Node tail;
/** Returns a list iterator object for the list at
* the specified index
*/
public DoublyLinkedList(){
}
private static class Node {
Node next = null;
Node prev = null;
E data;
public Node(E dataItem){
data = dataItem;
}
public Node(E dataItem, Node previous, Node nextNode){
this(dataItem);
prev = previous;
next = nextNode;
}
}
private class MyListIter implements ListIterator{
private Node lastReturned; // a link reference to the last item that was returned
private Node 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 newNode = new Node(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 iterator() {
// TODO Auto-generated method stub
return null;
}
}
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.
以上就是Cannot convert from Node to Node?的详细内容,更多请关注web前端其它相关文章!