Results 1 to 4 of 4
  1. #1
    summersab is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default 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):

    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;
    	}
    }
    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?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    See the API for java.util.Arrays, it contains a method for checking for array equality (Arrays.equals(array1, array2) )

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,801
    Rep Power
    25

    Default

    the program never executes that if statement
    Try debugging your code by printing out the values being tested:
    System.out.println("var1=" + var1 + "< var2=" + var2 + "<");

  4. #4
    summersab is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. equals(object obj)
    By hamed in forum New To Java
    Replies: 2
    Last Post: 09-07-2010, 08:08 AM
  3. Object class's equals() method behavior????
    By skyineyes in forum New To Java
    Replies: 4
    Last Post: 07-20-2008, 12:58 AM
  4. Replies: 1
    Last Post: 04-17-2008, 11:10 PM
  5. Replies: 0
    Last Post: 01-26-2008, 12:13 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
  •