1

first time commenter here. I'm trying to add char arrays to a Binary Tree in java. The problem is that when I try to make the char array into a node (prior to adding the node to the tree), the node is not a char array but a reference. Here is my making-things-a-node class:

public class TreeNode {

char[] data;
TreeNode left, right, parent;
public TreeNode(char[] data) {
    this.data = data;
    left = right = parent = null;
}
}

and here is the line I'm using to pass the char array to the node class:

TreeNode theNode = new TreeNode(dictionary);

where dictionary is a char array. And when I print theNode I get something like this: ([C@58623a98). I think the problem might be with the this.data but I'm not sure. Can anyone help me?

Update: when I print dictionary I get the char array I started with.

3
  • 2
    If you want to print the content of data, use Arrays.toString(data);
    – Alexis C.
    Mar 29, 2014 at 19:13
  • Look into printing arrays and the inherited Object#toString() method. Mar 29, 2014 at 19:13
  • The problem is not the printing of the array. That works fine. The problem is making the array into a node. Also, how would I go about making a copy in this particular case?
    – jani
    Mar 29, 2014 at 19:24

2 Answers 2

2

What you describe is normal behaviour. The outcome of printing an object is determined by its .toString() method. If no .toString() method is declared, the default .toString() method inherited from Object class is used, which gives the output you received.

If you want the array to look nice when printed, you can define your own .toString() method, or use an utility method of Arrays.toString(yourArray).

0

The observation is correct that each node has access to the same character array1; if you want a copy of any object, make a copy of an object.

Either of the following are sufficient to make a copy of a primitive array, although clone has a bad reputation in general.

this.data = Arrays.copyOf(data, data.length);
this.data = data.clone();

Alternatively, you could make it the callers responsibility to pass in different objects.

// i.e. when omitting the above
new TreeNode(dictionary.clone());

More complex objects/object-graphs will require different handling. My recommendation is to simply try and reduce mutable objects which eliminates the need for making the appropriate copies.


1 That is, objects are not copied/cloned/duplicated when they are passed in Java; rather, the value of the reference to the object is passed, and hence the same object is referred to.

3
  • That did not work, but it seems like it should. I still have the same output as before. Perhaps my problem lies not where I thought it did.
    – jani
    Mar 29, 2014 at 19:37
  • @jani Oh, I see. Your printing issue is unrelated to the above. Mar 29, 2014 at 19:38
  • @jani See stackoverflow.com/questions/409784/… Mar 29, 2014 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.