Results 1 to 10 of 10
- 04-06-2011, 05:52 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
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.
So neither fruitList.contains(new Fruit(element)) nor fruitList.contains(new Fruit(fruits[0])) returns true, which seems weird to me.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
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.
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().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
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!
- 04-06-2011, 05:56 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What happens if you print element?
- 04-06-2011, 05:59 AM #3
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
- 04-06-2011, 06:38 AM #4
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.My problem shouldn't be a method overwriting error because if I hard code a string in, it works.
db
- 04-06-2011, 06:51 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
"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.
- 04-06-2011, 07:01 AM #6
Read the contract of equals(...) and hashCode() in the documentation for Object.is doing both just good practice.
Could we see your implementation of Fruit#equals(...)?
db
- 04-06-2011, 07:22 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
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()); }
- 04-06-2011, 07:30 AM #8
OK so now could we see your toString() method? And why should equals(...) use a return value from toString() in the first place?
db
- 04-06-2011, 07:43 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
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.Java Code:public String toString(){ return 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
- 04-06-2011, 07:50 AM #10
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Binary-algorithm -> Insert String to sorted String-ArrayList
By Muskar in forum Advanced JavaReplies: 12Last Post: 11-26-2010, 08:33 AM -
problem searching for a String element in a paralell array
By david185000 in forum New To JavaReplies: 4Last Post: 07-27-2010, 08:52 PM -
What is the difference between Inline literal and Static final String in Java.
By devaru2003 in forum Advanced JavaReplies: 3Last Post: 03-26-2010, 07:09 AM -
String match to the linkedlist element
By jboy in forum New To JavaReplies: 3Last Post: 09-06-2009, 07:02 AM -
Convert Linked List Object element to String
By CirKuT in forum New To JavaReplies: 2Last Post: 12-13-2008, 05:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks