Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have been trying to write this method but I get false every time. This is what

ID: 3905677 • Letter: I

Question

I have been trying to write this method but I get false every time. This is what I have. I am not allowed to use iterator object.

public boolean contains(AnyType value) {
Node<AnyType> cursor = header;
while ((cursor != null) && (!cursor.value().equals(value)))
{
cursor = cursor.getNext();
}
return cursor != null;
}

boolean contains(AnyType value) This method returns true if this list contains the specified value, otherwise it returns false

This is for a doubly linked list. Thankyou in advance !

Explanation / Answer

The problem here is with the following conitiion in while cursor.value().equals(value)

equals() always compares the references (that is they are equal if they both point to same memory location) that is it will always return false eventhough the values are equal. You need to compare their content rather than their memory references. So it is not good to use equals(). It would be good if you provide you class definition to help.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote