昨日、このコードについて質問したところ、その問題を解決するのに役立ちましたが、現在、他の問題が発生しています。私は双方向にリンクされたリストを取得し、追加、削除、リセットなどの関数を作成しています。私は削除を除いてすべてのために働くべきものを持っています(私はそこにエラーがあるかどうかはよくわかりません)が、主な問題は私が得続けるエラーです:
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);
}
}