我正在写一个基本的链表程序。为了测试它,我刚刚创建了一个基本的列表,添加了一堆东西然后尝试删除它们。它引发了错误,但我并不理解。
该计划:
public static void main(String[] args) {
OrderedLinkedList one = new OrderedLinkedList();
for(int i = 0; i<=20; i=i+2){
one.add(i);
}
one.remove(3);
//System.out.println(one.toString());
}
我想知道如果我试图删除那里没有的对象会发生什么。但是当我这样做时抛出了这个异常:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
异常似乎发生在包含compareTo()
方法的行中的remove方法中。我如何尝试将int转换为字符串?当它传递给方法时,不应该是可比较的吗?
public boolean add(Comparable obj)
{
if(head.next == null){
OrderedListNode newNode = new OrderedListNode(obj, head, tail);
}
else{
OrderedListNode newNode = new OrderedListNode(obj,tail.previous,tail);
tail.previous.next = newNode;
tail.previous = newNode;
}
return true;
}
public boolean remove(Comparable obj)
{
OrderedListNode temp = head;
while(temp != null){
if(temp.theItem.compareTo(obj) == 0){
temp.previous.next = temp.next;
temp.next.previous = temp.previous;
}
else{
temp = temp.next;
}
}
return true;
}
/**
* Empty this OrderedLinkedList.
*/
public void clear()
{
// reset header node
head = new OrderedListNode("HEAD", null, null);
// reset tail node
tail = new OrderedListNode("TAIL", head, null);
// header references tail in an empty LinkedList
head.next = tail;
// reset size to 0
theSize = 0;
}
private class OrderedListNode{
Comparable theItem;
OrderedListNode next;
OrderedListNode previous;
public OrderedListNode(Comparable theItem, OrderedListNode previous, OrderedListNode next){
this.theItem = theItem;
this.next = next;
this.previous=previous;
}
}
@Override
public String toString()
{
String s = "";
OrderedListNode currentNode = head.next;
while (currentNode != tail)
{
s += currentNode.theItem.toString();
if (currentNode.next != tail)
{
s += ", ";
}
currentNode = currentNode.next;
}
return s;
}
答案 0 :(得分:0)
你的问题在于清洁方法。它实例化字符串数据类型节点并将其放入尾部和头部。因此,当调用remove()时,当它在命中字符串类型的节点时迭代/循环遍历对象列表时,抛出了异常,因为它试图将int与string进行比较。
无论如何,我通过重新编写部分代码来解决您的问题。它并不完美,但它至少不再是问题
以下是更改。
1st:在remove方法中 - 你需要一个break语句,否则代码会进入无限循环。
第二:我已经重新设计了Clean方法,因为它包含一些逻辑错误。基本上你会想要在清理过程中将东西设置为null或0,而不是为它初始化一个新的尾部和头部对象。
第三:添加逻辑也有点不正确,逻辑上明智。我还重写了它。
注意:由于时间限制,我没有真正测试我自己的更改,因此可能存在仍然存在的错误,这不在此问题的范围内。
如果您发现任何错误,只需发送回复邮件,我就会抽出时间重新审视一下。
public class OrderedLinkedList {
OrderedListNode head = null;
OrderedListNode tail = null;
int theSize = 0;
public boolean add(Comparable obj)
{
if(head == null){
head = new OrderedListNode(obj, head, tail);
}
else{
OrderedListNode newNode = new OrderedListNode(obj, /*prev=*/tail, /*next=*/null);
if (tail == null)
head.next = newNode;
else {
tail.next = newNode;
// tail.previous = newNode; No need this line. Already set in constructor
}
tail = newNode;
}
return true;
}
public boolean remove(Comparable obj)
{
OrderedListNode temp = head;
boolean elemRemoved = false;
while(temp != null){
if(temp.theItem.compareTo(obj) == 0) {
// dereference this object for garbage collection
if (temp.previous != null)
// Not a head - Head will not have previous
temp.previous.next = temp.next;
temp.next.previous = temp.previous;
elemRemoved = true;
break;
}
else{
temp = temp.next;
}
}
return elemRemoved;
}
/**
* Empty this OrderedLinkedList.
*/
public void clear()
{
// reset header node
head = null;
// reset tail node
tail = null;
// header references tail in an empty LinkedList
//head.next = tail;
// reset size to 0
theSize = 0;
}
private class OrderedListNode{
Comparable theItem;
OrderedListNode next;
OrderedListNode previous;
public OrderedListNode(Comparable theItem, OrderedListNode previous, OrderedListNode next){
this.theItem = theItem;
this.next = next;
this.previous=previous;
}
}
@Override
public String toString()
{
String s = "";
OrderedListNode currentNode = head.next;
while (currentNode != tail)
{
s += currentNode.theItem.toString();
if (currentNode.next != tail)
{
s += ", ";
}
currentNode = currentNode.next;
}
return s;
}
}
public class main {
public static void main(String[] args) {
OrderedLinkedList one = new OrderedLinkedList();
one.clear();
for(int i = 0; i<=20; i=i+2){
one.add(i);
}
boolean removedOK = one.remove(3);
System.out.println("Attempt 1 : Something was removed => " + removedOK);
removedOK = one.remove(2);
System.out.println("Attempt 2: Something was removed => " + removedOK);
removedOK = one.remove(4);
System.out.println("Attempt 3: Something was removed => " + removedOK);
}
}