Java 在链表中重写等于
我一直在寻找解决我问题的办法。一直在阅读大量关于重写equals以正确比较链表节点的内容,但现在已经卡住了。基本上,我试图搜索我的列表,找到一个与之相等的节点并删除它。我已经找到了查看链表的方法,但是当我返回节点时,它只是胡言乱语。我正试图找到一种方法,将其放入一个字符串中,并将其与另一个字符串进行比较。无论如何,这里是代码和我的equals方法,目前不起作用Java 在链表中重写等于,java,list,linked-list,Java,List,Linked List,我一直在寻找解决我问题的办法。一直在阅读大量关于重写equals以正确比较链表节点的内容,但现在已经卡住了。基本上,我试图搜索我的列表,找到一个与之相等的节点并删除它。我已经找到了查看链表的方法,但是当我返回节点时,它只是胡言乱语。我正试图找到一种方法,将其放入一个字符串中,并将其与另一个字符串进行比较。无论如何,这里是代码和我的equals方法,目前不起作用 public class MagazineList { private MagazineNode list; priva
public class MagazineList {
private MagazineNode list;
private Object obj;
public MagazineList(){
list = null;
}
public void add(Magazine mag){
MagazineNode node = new MagazineNode(mag);
MagazineNode current;
if(list==null)
list = node;
else{
current = list;
while(current.next != null)
current = current.next;
current.next = node;
}
}
public void insert(Magazine mag)
{
MagazineNode node = new MagazineNode (mag);
// make the new first node point to the current root
node.next=list;
// update the root to the new first node
list=node;
}
public void deleteAll(){
if(list == null){
}
else{
list = null;
}
}
public void delete (Magazine mag) {
MagazineNode current = this.list;
MagazineNode before;
//if is the first element
if (current.equals(mag)) {
this.list = current.next;
return; //ending the method
}
before = current;
//while there are elements in the list
while ((current = current.next) != null) {
//if is the current element
if (current.equals(mag)) {
before.next = current.next;
return; //endind the method
}
before = current;
}
//it isnt in the list
}
public boolean equals(Object other) {
System.out.println("Here in equals" + other + this);
// Not strictly necessary, but often a good optimization
if (this == other)
return true;
else{
return false;
}
}
@ Override
public String toString(){
String result = " ";
MagazineNode current = list;
while (current != null){
result += current.magazine + "\n";
current = current.next;
}
return result;
}
private class MagazineNode {
public Magazine magazine;
public MagazineNode next;
public MagazineNode(Magazine mag){
magazine = mag;
next = null;
}
}
}
您的删除方法看起来不错,只是不应该将MagazineNode与杂志进行比较。你应该把一本杂志和一本杂志进行比较
将if current.equalsmag替换为if current.magage.equalsmag.确定-注意,equals方法只是重新实现==标识equals 这意味着如果另一个不是相同的库对象,则此操作将失败。如果这是您想要的方式,那很好,但通常您希望在杂志中选择属性 所以,如果杂志有一个字符串标题,在equals方法中,您可以执行以下操作:
if (magazine instanceof Magazine && magazine.getTitle().equals(other.getTitle()) returnval = true;
另外,请参阅Joshua Bloch的有效Java,以获得对此的详细描述。每当重写equals方法时,您都希望重写hashCode方法。他们走在一起,他描述了原因
希望这有帮助。如果您使用的是eclipse,请使用以下函数:eclipse->Source->Generate hashCode and equals。并研究其实施。这将帮助您理解如何编写equals和hashCode。祝你好运。你为什么不使用java api中可用的链表实现之一?是的,不允许。这是计算机科学课,他们试图向我们展示API能为我们做什么,并说我们需要在短方法之前知道长方法,我这样做,输出是相同的,但它仍然返回false它没有任何意义为什么会发生这是相同的输出返回false是什么?删除返回无效。它怎么会返回false呢?你说的是一个输出,但是什么的输出?您正在调用什么方法,使用哪些参数,以及在包含什么的列表上调用什么?结果如何?Magazine.equals是如何定义的?我正在使用print来找出程序中发生了什么,它是打印错误的。我没有定义equals,只是使用了它的预定义版本。该列表只包含字符串,我可以输入相同的字符串,它仍然返回false。如果两个比较对象是完全相同的实例,则equals的dafault版本仅返回true。您的列表不包含字符串实例,但包含杂志实例。如果您希望一个杂志与另一个杂志相等,如果它包含相同的字符串,那么您需要在杂志类中重写equals。老实说,这非常有用。这是非常困难和抽象的理解,但看看它是有意义的