0

Here is my code:

public class RecipeBook {
private class Node {
    private Recipe mData;
    private Node mNext;
    private Node mPrev;
    public Node(Recipe data, Node next, Node prev) {
        mData = data;
        mNext = next;
        mPrev = prev;
    }
}

private Node mHead;
private Node mTail;
private int mCount;
public RecipeBook() {
}
public void add(Recipe itemToAdd) {
    Node newNode = new Node(itemToAdd, null, null);
    if (mHead == null)
        mHead = mTail = newNode;
    else {
        mTail.mNext = newNode;
        newNode.mPrev = mTail;
        mTail = newNode;
   }
   mCount++;
}
public int getCount() {
   return mCount;
}
public Recipe get(int index) {
   Node n = mHead;
   for (int i = 0; i < index; i++) {
   if (n == null)
       return null;
   n = n.mNext;
   }
   return n != null ? n.mData : null;
}
public boolean remove(Recipe itemToRemove) {
   Node n = mHead;
   boolean check = false;
   if (n == null) {
       check = false;
       return check;
   }
   while (n != null) {
     if (n.mData ==  itemToRemove) {
         if (n == mHead) {
             mHead = mHead.mNext;
             mHead.mPrev = null;
         }
         else if (n == mTail) {
             mTail = mTail.mPrev;
             mTail.mNext = null;
         }
         else {
            Node prev = n.mPrev;
            Node next = n.mNext;
            prev.mNext = next;
            next.mPrev = prev;
         }
         check = true;
         mCount--;
         return check;
     }
     else {
         n = n.mNext;
     }
   }
   return check;
}
public Recipe searchByName(String name) {
   for (int i = 0; i < this.getCount(); i++) {
       if (this.get(i).getName().equals(name))
           return this.get(i);
   }
    return null;
  }
public Recipe searchByIngredients(String target) {
   for (int i = 0; i < this.getCount(); i++) {
       if (this.get(i).hasIngredients(target) == true)
           return this.get(i);
   }
   return null;
}
}

My problem is with the remove method, and I've actually checked with the debugger. The while loop works fine, and the program goes through the if statement when n.mData == itemToRemove. It removes what it's suppose to remove, no matter where the item is in the list. In addition the mCount gets smaller by one, and check becomes true, but once it gets to return it doesn't return anything. Instead it goes back to the loop, and keeps going until n is null. This would have been fine if check was remaining true, but after it goes out of the if statement without returning anything, it reverts check back to false which then makes the whole method return false. I've tried removing the return and having break which gave me the same result. I've also tried not having a return nor break while running the debugger which showed that it remained in the if statement, as if that statement was a loop.

4

1 回答 1

0

这是一个临时解决方案,但尝试添加breakafter when n.mData == itemToRemove,然后 return check。这应该能够给你一些提示,也许你的代码为什么不工作。

public boolean remove(Recipe itemToRemove) {
   Node n = mHead;
   boolean check = false;
   if (n == null) {
       check = false;
       return check;
   }
   while (n != null) {
     if (n.mData ==  itemToRemove) {
         if (n == mHead) {
             mHead = mHead.mNext;
             mHead.mPrev = null;
         }
         else if (n == mTail) {
             mTail = mTail.mPrev;
             mTail.mNext = null;
         }
         else {
            Node prev = n.mPrev;
            Node next = n.mNext;
            prev.mNext = next;
            next.mPrev = prev;
         }
         check = true;
         mCount--;
         break;
     }
     else {
         n = n.mNext;
     }
   }
   return check;
}
于 2013-10-19T06:08:08.113 回答