在链表中使用boolean remove(Object o)

时间:2014-12-26 14:55:36

标签: java

这是remove()方法

public boolean removeId(Object d) {
    boolean removed=false;
    curr=fNode; 

    if(curr==null)
      throw new RuntimeException("cannot delete");

    else if(curr.data==d) {       

        fNode=fNode.next;
        curr=fNode;
        removed=true;
      }

    else  
      curr=curr.next;


    count--;
    return removed;
}

班级

public class Hospital {

    private int id;
    private String name;
    private double price;
    private String treatType;
    private String date;

    public Hospital()  {
        id=0;
        name="NA";
        price=0;
        treatType="NA";
        date="NA";    
    }

    public Hospital(int a,String b,double c,String d,String e) {
        id=a;
        name=b;
        price=c;
        treatType=d;
        date=e;
    }

    public void setHospital(int a,String b,double c,String d,String e) {
        id=a;
        name=b;
        price=c;
        treatType=d;
        date=e;
    }

    public int getId(){ return id; }
    public String getName(){ return name; }
    public double getPrice(){ return price; }
    public String getTreatType(){ return treatType; }
    public String getDate(){ return date; }

    public String toString()  {
        return ("Id :"+id+"\nName :"+name+"\nPrice :"+price+"\nTreatment type :"+treatType+"\nDate :"+date);
    }
  }

主要

System.out.println("Enter id you want to remove :");
int remId=in.nextInt();

list.removeId(new Integer(remId));

为什么第一个节点中的id没有被删除?我尝试删除第一个节点但它没有删除它。我尝试了很多方法,包括将remId更改为int但它仍然不起作用。

现在,我通过消除'while'并使用'equal'来对remove()方法进行一些更改,但它没有任何意义。然后我尝试了另一个改变它的工作原理 为什么这段代码有效?

public boolean removeId(Object d)
{
    boolean removed=false;

    curr=fNode;
        if(curr==null)
            throw new RuntimeException("cannot delete");

        else
            {
                    fNode=fNode.next;
                    curr=fNode;
                    removed=true;
            }


            curr=curr.next;

    count--;
    return removed;
}

5 个答案:

答案 0 :(得分:3)

原因是您要将Integer对象与==进行比较。请改用equals()方法。

使用==比较Java中的对象时,将它们与 identity 进行比较,而不是 equality 。当且仅当两个操作数都是完全相同的对象时,==才会产生true。它会在任何其他情况下产生false,其中包括两个操作数相等但不相同的情况。

您的代码正在使用Integer,这是一个类,因此您可以创建两个相同但不相同的Integer个对象:

public static void equalsVsSameIntegerDemo() {
    final Integer i1 = new Integer(1);
    final Integer i2 = new Integer(1);
    System.out.println(i1.equals(i2)); // true
    System.out.println(i1 == i2); // false
}

或者,如果您知道它Integer且不能null,则可以使用int代替Integer。但这只适用于您在int - 方法中使用remove的情况。如果您在来电者中使用int并致电remove(Object),则int会转换为Integer。如果您的ID非常通用,那么您希望使用equals()代替==

答案 1 :(得分:1)

在您的代码中,您有if (curr.data==d)

但你确定new Integer(remId) == theObjectInsertedInTheList

我不这么认为,这就是你的问题所在。

更多信息:Object Equality

答案 2 :(得分:1)

您应该将节点的数据与equals进行比较,而不是与==进行比较,这只会检查两者是否是同一个实例:

if(curr.data.equals(d))

答案 3 :(得分:1)

您正在使用对象标识来测试相等性:

if (curr.data == d)

仅当两个操作数是相同的对象时才是真的,它们不是。将该行更改为:

if (curr.data.equals(d))

比较两个对象的(至少对于Integer类)。

答案 4 :(得分:0)

请注意,即使您使用正确的比较,您的算法仍会失败,因为fNodecurr并未一起移动。

删除时,您要删除链中的给定链接 - 请注意该链是由以下链接组成的:A --> B --> C --> D。删除C意味着删除指向B --> C的链接,并将其替换为指向B --> D的链接。 所以你去 A --> B --> C --> DA --> B --> D

算法,伪代码即可。

 1. start at the beginning of the list with the first node (Head of the list).
 2. check if it is the one you want to delete.
 3. if not, go to the next node and repeat step 2 
 4. if so, make the next node of the previous node the next node of this node
 5. remove the reference from this node to the next node