//Return true if the given element exists within the list and false otherwise.
public boolean contains(E element)
{
Node<E> cursor = head;
for(int x = 0; x < size; x++)
{
if ( cursor == null )
{
return false;
}
else if (cursor.content == element)
{
return true;
}
else
{
cursor = cursor.next;
}
}
return false;
}
3 Replies - 20100 Views - Last Post: 19 April 2013 - 09:31 AM
#1
LinkedList.contains Method implementation always returns false
Posted 18 April 2013 - 10:24 PM
Just finished implementing the LinkedList, but i'm having a problem with the contains method; the problem is that whenever i call the function it always returns false.Somebody please help me.
Replies To: LinkedList.contains Method implementation always returns false
#2
Re: LinkedList.contains Method implementation always returns false
Posted 18 April 2013 - 11:33 PM
This code seems correct.
To know what's up I need more code
Also you should consider using
in line 12. By default this does the same as "==", but allows people to decide (by overwriting the equals() method) how to compare objects (you included - it may be the case that you're comparing objects that have the same state data, but have different location in memory and thus will not trigger '=='.
To know what's up I need more code
- The code where you call the contains() method on some LinkedList.
- The implementation of the method that inserts into the LinkedList.
Also you should consider using
else if (cursor.content.equals(element))
in line 12. By default this does the same as "==", but allows people to decide (by overwriting the equals() method) how to compare objects (you included - it may be the case that you're comparing objects that have the same state data, but have different location in memory and thus will not trigger '=='.
#3
Re: LinkedList.contains Method implementation always returns false
Posted 19 April 2013 - 03:52 AM
The code is kind of complicated for nothing and shows that you have a total lack of confidence in your own code.
If you maintain the size of your LinkedList in an instance variable why do you test in the loop id the Node is null ? It just can't !!!
As mentionned by ChrisGulddahl you can't compare 2 objects, like E, using the == operator so your code could be
Using the for() loop and the size
or using the forward pointer which is a more common practice
Happy coding
If you maintain the size of your LinkedList in an instance variable why do you test in the loop id the Node is null ? It just can't !!!
As mentionned by ChrisGulddahl you can't compare 2 objects, like E, using the == operator so your code could be
Using the for() loop and the size
public boolean contains(E element)
{
Node<E> cursor = head;
for(int x = 0; x < size; x++)
{
if (cursor.content.equals(element))
return true;
cursor = cursor.next;
}
return false;
}
or using the forward pointer which is a more common practice
public boolean contains(E element)
{
Node<E> cursor = head;
while(cursor != null)
if(cursor.content.equals(element))
return true;
cursor = cursor.next;
}
return false;
}
Happy coding
This post has been edited by pbl: 19 April 2013 - 09:39 AM
Reason for edit:: Remove a useless null test on the for()
#4
Re: LinkedList.contains Method implementation always returns false
Posted 19 April 2013 - 09:31 AM
pbl, on 19 April 2013 - 03:52 AM, said:
The code is kind of complicated for nothing and shows that you have a total lack of confidence in your own code.
If you maintain the size of your LinkedList in an instance variable why do you test in the loop id the Node is null ? It just can't !!!
As mentionned by ChrisGulddahl you can't compare 2 objects, like E, using the == operator so your code could be
Using the for() loop and the size
or using the forward pointer which is a more common practice
Happy coding
If you maintain the size of your LinkedList in an instance variable why do you test in the loop id the Node is null ? It just can't !!!
As mentionned by ChrisGulddahl you can't compare 2 objects, like E, using the == operator so your code could be
Using the for() loop and the size
public boolean contains(E element)
{
Node<E> cursor = head;
for(int x = 0; x < size; x++)
{
if (cursor.content.equals(element))
return true;
cursor = cursor.next;
}
return false;
}
or using the forward pointer which is a more common practice
public boolean contains(E element)
{
Node<E> cursor = head;
while(cursor != null)
if(cursor.content.equals(element))
return true;
cursor = cursor.next;
}
return false;
}
Happy coding
Thanks a lot!
This post has been edited by pbl: 19 April 2013 - 09:40 AM
Reason for edit:: Removed a useless test for null in the for()
Page 1 of 1

New Topic/Question
Reply


MultiQuote



|