Results 1 to 4 of 4
Thread: Trouble with Object.equals()
- 06-14-2011, 12:24 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
Trouble with Object.equals()
Okay, so I'm using a DefaultTreeModel which uses DefaultMutableTreeNodes. The datatype stored in my DefaultMutableTreeNode elements is a string array, i.e. String[]. I'm trying to compare elements of my tree to a string array populated from input data. So, this is what my code looks like (some things may not be declared - this is just a random pull from my program):
My problem is with the final if statement where it checks for equality. I've tried to cast the objects as strings, string arrays, and other things. I've tried everything I can think of, but even when I'm looking at the variables in Eclipse and I KNOW they're equal, the program never executes that if statement. What am I missing?Java Code:String[] leaf = [populated with input data]; DefaultMutableTreeNode child = new DefaultMutableTreeNode(leaf); DefaultMutableTreeNode parent = new DefaultMutableTreeNode("root"); boolean childExists = false; for (int n = 0; n < tree.getChildCount(parent); n++) { DefaultMutableTreeNode iterateNode = (DefaultMutableTreeNode) tree.getChild(parent, n); Object iterateChild = iterateNode.getUserObject(); Object currentChild = child.getUserObject(); if (iterateChild.equals(currentChild)) { childExists = true; break; } }
- 06-14-2011, 01:00 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
See the API for java.util.Arrays, it contains a method for checking for array equality (Arrays.equals(array1, array2) )
- 06-14-2011, 01:06 AM #3
Try debugging your code by printing out the values being tested:the program never executes that if statement
System.out.println("var1=" + var1 + "< var2=" + var2 + "<");
- 06-14-2011, 01:07 AM #4
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
equals(object obj)
By hamed in forum New To JavaReplies: 2Last Post: 09-07-2010, 07:08 AM -
Object class's equals() method behavior????
By skyineyes in forum New To JavaReplies: 4Last Post: 07-19-2008, 11:58 PM -
[SOLVED] If a object equals another object, do they contain the same data?
By bobleny in forum New To JavaReplies: 1Last Post: 04-17-2008, 10:10 PM -
name clash: equals(E) in and equals(java.lang.Object)
By AdRock in forum New To JavaReplies: 0Last Post: 01-25-2008, 11:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks