Results 1 to 10 of 10
  1. #1
    Dalqin is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Question Difference between String and String[] element

    I'm having a discrepancy between a String I hard code and a String[] array with which I extract a String element from. I'd like to use the String[] array, which I'm filling by using the String.split() method. The array is filling properly and its elements show to be exactly as they should via debug readouts.

    So this is what I'm doing, essentially. Just wanting to use a string representation of my object name to see if that object is present in a list. I'll detail the problem out afterwards.
    Java Code:
    ArrayList<Fruit> fruitList = new ArrayList<Fruit>();
    fruitList.add(new Apple());   //all of these are extensions of the class Fruit
    fruitList.add(new Orange());
    fruitList.add(new Pear());     
    
    String wholeString = "apple orange pear";
    String[] fruits = String[5];   //extra size just to be safe for testing
    fruits = wholeString.split(" "); //splits the string up by space characters
    
    
    String element = fruits[0]   //assigns "apple" to the String element
    
    
    System.out.println(fruitList.contains(new Fruit(element)));//returns false
    System.out.println(fruitList.contains(new Fruit(fruits[0])));//returns false
    So neither fruitList.contains(new Fruit(element)) nor fruitList.contains(new Fruit(fruits[0])) returns true, which seems weird to me.
    Class Fruit takes a string as a parameter, which is assigned to a String instance variable for use with equals() methods, which are written to compare strings that represent the name of the respective object. My problem shouldn't be a method overwriting error because if I hard code a string in, it works.
    These below approaches all work, where as the one listed above does not.
    Java Code:
    fruitList.contains(new Fruit("apple")); //returns true for me when tried
    
    String temp = "apple";
    fruitList.contains(new Fruit(temp)); //also returns true for me when tested
    So what gives? I've gotten this to work splendidly when I avoid the String[] array, so my equals() method should be fine when I call contains() on my list. I just don't see the difference between a hard coded String a String pulled from a String[] array that was filled using .split().

    Any insight would be wonderful. I've spent enough time toying with this that I've run out of idea's for what's going on. :D Thank you much!

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What happens if you print element?

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    fruitList.add(new Apple());
    // above line stores an Apple object in the List
    
    System.out.println(fruitList.contains(new Fruit(element)));
    // above line looks for a Fruit object which holds the String "apple" in the List
    // obviously the two things are different

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default

    My problem shouldn't be a method overwriting error because if I hard code a string in, it works.
    I don't see any logic at all in that. did you override equals() [and hashCode()] in your Fruit class? I'm betting you didn't.

    db

  5. #5
    Dalqin is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    "apple" is what prints when I print out element.

    I over wrote equals methods and they work, I'm positive on that. I didn't write make my own hashCode method to override the parent ones. I was under the assumption this was only used for hashTables. Does that mean .contains() uses .hashCode()? or is doing both just good practice.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default

    is doing both just good practice.
    Read the contract of equals(...) and hashCode() in the documentation for Object.

    Could we see your implementation of Fruit#equals(...)?

    db

  7. #7
    Dalqin is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    This is what I wrote. I think I said this, but just to cover everything I'll list it out.
    Class Fruit has an instance variable - private String name;
    The constructor takes in a String as a parameter and sets it to this name variable.

    Apple, Orange, and Pear extend Fruit with constructors that just super("apple"), super("orange"), or super("pear") respectively.

    Equals method is as follows, written just in my Fruit class for the others to share.
    Java Code:
    public boolean equals(Object o){
         return this.name.equals(o.toString());
    }

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default

    OK so now could we see your toString() method? And why should equals(...) use a return value from toString() in the first place?

    db

  9. #9
    Dalqin is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Java Code:
    public String toString(){
    	return name;
    }
    equals() needs to take in an object as a parameter in order to override things, right? I'm only going to be passing in strings to do a comparison between string representations of objects and objects themselves. I think casting the equals() object parameter to a String would give errors, and I just hate casting and try to avoid it. So I invoked toString() in the object parameter (which will always be a String if I'm careful) to get the name variable of my Fruit (the one I'm checking the list against) with which to compare to this.name.

    It's just weird to me that if I hardcode fruitList.contains(new Fruit("apple")) it works, and trying to use my String[] element (that shows to have "apple" as the element I'm trying to use) doesn't work. but if hashCode rewriting is required whenever an equals rewrite is done, then I'm missing that part. I'm reading about the relationship they share in between posts. :p

  10. #10
    Dalqin is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Built my own hashCode() method and it works. Amazing call. Thank you so much for pointing me in a direction and for everyone taking the time to respond to my post. Very prompt and excellent, thanks again! I've some reading to do it seems. >.>

Similar Threads

  1. Replies: 12
    Last Post: 11-26-2010, 09:33 AM
  2. Replies: 4
    Last Post: 07-27-2010, 09:52 PM
  3. Replies: 3
    Last Post: 03-26-2010, 08:09 AM
  4. String match to the linkedlist element
    By jboy in forum New To Java
    Replies: 3
    Last Post: 09-06-2009, 08:02 AM
  5. Convert Linked List Object element to String
    By CirKuT in forum New To Java
    Replies: 2
    Last Post: 12-13-2008, 06:22 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •