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)) returns true, which seems weird to me.
ArrayList<Fruit> fruitList = new ArrayList<Fruit>();
fruitList.add(new Apple()); //all of these are extensions of the class Fruit
String wholeString = "apple orange pear";
String fruits = String; //extra size just to be safe for testing
fruits = wholeString.split(" "); //splits the string up by space characters
String element = fruits //assigns "apple" to the String element
System.out.println(fruitList.contains(new Fruit(element)));//returns false
System.out.println(fruitList.contains(new Fruit(fruits)));//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().
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!