• Post Reply Bookmark Topic Watch Topic
  • New Topic

LinkedList contains(toString) and isEmpty() seem to disagree  RSS feed

 
Richard Warner
Greenhorn
Posts: 16
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey folks! Is this a bad "print to console" approach or bad "list populating" approach or something else completely?
Lines 9 and 10 are having a little "he said, she said" moment and line 3 is what I hear crying to me from the console.

Oh, and I know it's not shown here (trying to be terse with the code here) but my abstract class Card (which is...
I guess... "nested" for now) is the only buckaroo with a toString method I have, and the compiler seems to be finding it just fine.



Thanks!
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a hard time understanding what you are asking. You seem to have asked a question in your subject -- "LinkedList contains(toString) and isEmpty() seem to disagree-am I not actually populating this list?" -- but why do you think contains(toString) and isEmpty() should agree? Couldn't you have a non-empty string that doesn't contain the toString()?

And this is just my personal taste but I think you'll go further being clear rather than cutsie. For instance, what did line 9 actually print?
 
Mark King
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is neither a populating problem, nor a print to console problem. The problem is that you are not using the contains method correctly.
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since your class is a LinkedList<Card>, it can only contain Card objects. So when you ask whether it contains a String object -- the toString() method returns a String, so that's what you are asking -- you can be certain that it doesn't contain that object.

I can't quite see what you are trying to achieve with that call to the contains() method; you need to pass it a Card object but you can't tell what kind of Card object might be in it, because you've been putting random Card objects into the list. It looks like you're trying to test your code in some way, but I can't tell what the test is supposed to be.
 
Richard Warner
Greenhorn
Posts: 16
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking that since my abstract class Card was the only one containing a toString method that toString would be referencing each inheriting card's class name, then seeing if it contained that class of object. I was expecting contains() to give me a report of true, but as you say, I must be trying to use it for something other than its intended purpose. So, my problem is that I'm generating a String (hopefully a unique String of each card I'm testing) but contains() is testing for an Object? Even if the name is identical, different data types are apples and oranges, I see. Sorry all. Thanks for the feedback.
 
Richard Warner
Greenhorn
Posts: 16
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Plus, like you say, I've been generating all Cards. Even polymorphed through randomly calling their subclasses, they'll all register as Card Objects when tested, not the variety of sub-Class Objects! Thanks, that's much clearer, I think I can tackle this better.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a LinkedList<Card> and you want to check if it contains a certain card, then you should pass the contains() method a Card, not a String. The contains() method is not smart enough to understand "oh, he's giving me a String, so I should call toString() on all the cards that I have to check if one matches".

You must also implement the equals() method in class Card to check if two cards are equal - the contains() method of the List will call the equals() method to check if the cards in the list match the card you're looking for.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!