0

我昨天问了一个关于这段代码的问题,它帮助我克服了这个问题,但是现在我遇到了其他问题。我正在使用一个双向链表并为其编写添加、删除、重置等功能。除了删除(我不太确定那里的错误)之外,我有什么应该适用于所有事情,但主要问题是我不断收到的错误:

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to support.DLLNode
at Double.add(Double.java:155)
at Double.main(Double.java:172)

我正在寻找有关如何解决此错误并修复 remove 方法的最后一部分的建议。这是代码:

import ch06.lists.*;
import support.*;



public class Double<T extends Comparable<T>> implements ListInterface<T> {

protected DLLNode<T> front; //Front of list
protected DLLNode<T> rear; //Rear of list
protected DLLNode<T> curPosition; //Current spot for iteration
protected int numElements; //Number of elements in list

public Double() {
    front = null;
    rear = null;
    curPosition = null;
    numElements = 0;
}

protected DLLNode<T> find(T target) {
    //While the list is not empty and the target is not equal to the current element
    //curr will move down the list. If curr becomes null then return null.
    //If it finds the element, return it.
    DLLNode<T> curr;
    curr = front;
    T currInfo = curr.getInfo();
    while(curr != null && currInfo.compareTo(target) != 0) {    
        curr = (DLLNode<T>)curr.getLink();      
    }
    if (curr == null) {
        return null;
    }
    else {
        return curr;
    }           
}

public int size() { 
    //Return number of elements in the list 
    return numElements;     
}

public boolean contains(T element) {
    //Does the list contain the given element?
    //Return true if so, false otherwise.
    if (find(element) == null) {
        return false;
    }
    else {
        return true;
    }
}

public boolean remove(T element) {
    //While the list is not empty, curr will move down. If the element can not
    //be found, then return false. Else remove the element.
    DLLNode<T> curr;
    curr = front;

    while(curr != null) {   
        curr = (DLLNode<T>)curr.getLink();      
    }
    if (find(element) == null) {
        return false;
    }
    else {
        if (curr == null) {
            curr = curr.getBack();
            curr = rear;
        }
        else if (curr == front) {
            curr = (DLLNode<T>)curr.getLink();
            curr = front;
        }
        else if (curr == rear) {
            curr = curr.getBack();
            curr = rear;
        }
        else {
            //curr.getLink().setBack(curr.getBack());
            //curr.getBack().setLink(curr.getLink());

        }
    return true;
    }
}   

public T get(T element) {
    //Return the info of the find method.
    if (find(element) == null) {
        return null;
    }
    else {
        return find(element).getInfo();
    }

}

public String toString() {
    DLLNode<T> curr;
    curr = front;
    String ans = "";
    while (curr != null) {
        ans = ans + " " + curr.getInfo();
        curr = (DLLNode<T>)curr.getLink();
    }
    return ans;
}

public void reset() {
    //Reset the iteration back to front
    curPosition = front;        
}

public T getNext() {
    //Return the info of the next element in the list
    DLLNode<T> curr;
    curr = front;
    //while (curr != null) {
        //curr = (DLLNode<T>)curr.getLink();
    //}
    if ((DLLNode<T>)curr.getLink() == null) {
        return null;
    }   
    else {
        curr = (DLLNode<T>)curr.getLink();
        return curr.getInfo();
    }       
}

public void resetBack() {

}

public T getPrevious() {
    //Return the previous element in the list
    DLLNode<T> curr;
    curr = front;
    if ((DLLNode<T>)curr.getLink() == null) {
        return null;
    }
    else if((DLLNode<T>)curr.getBack() == null) {
        return null;
    }
    else {
        curr = curr.getBack();
        return curr.getInfo();
    }
}

public void add(T element) {
    //PreCondition: Assume the element is NOT already in the list
    //AND that the list is not full!
    DLLNode<T> curr;                
    DLLNode<T>  newNode = (DLLNode<T>) element;
    curr = front;
    if (curr == null) {
        front = (DLLNode<T>)element;
    }
    else {
        newNode.setBack(curr.getBack());
        newNode.setLink(curr);
        curr.getBack().setLink(newNode);
        curr.setBack(newNode);
        curr = newNode;
    }

}
public static void main(String[] args){

    Double<String> d = new Double<String>();
    d.add("Hello");
    d.add("Arthur");
    d.add("Goodbye");
    d.add("Zoo");
    d.add("Computer Science");
    d.add("Mathematics");
    d.add("Testing");

    System.out.println(d);


}  

}
4

2 回答 2

0

直接的问题在于add(T element)您正在投射的方法:

DLLNode<T> newNode = (DLLNode<T>) element;

但是你通过elementaString不是 a DLLNode。查看您的主要方法:d.add("Hello");

添加时,请执行以下操作:DLLNode newNode = new DLLNode(element);

您可以查看 java LinkedList源代码(以获取有关实现的灵感以及针对您的问题正确使用泛型)。

于 2013-04-13T16:23:23.710 回答
0

在你的add(T element)方法中,你有一条线

DLLNode<T>  newNode = (DLLNode<T>) element;

这个演员表不起作用。

显然,您希望创建一个包含元素作为信息的新节点,以便在此处添加到您的列表中。

你想要的大概是

DLLNode<T>  newNode = new DLLNode<T>();
newNode.setInfo(element);

(我假设你DLLNode有这个二传手,因为它有其他二传手。)

于 2013-04-13T16:23:24.210 回答