DefaultMultableNode

If I actually use DefaultMultableNode class, how do i construct a tree node thaT can store smth? for example an array?
Can I do:

DefaultMultableNode thisOne = new DefaultMutableNode(int[] anArray); ??  Would this work? Will the class recognise this constructor?
jtcyAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
int[] pcopy = (int[]) player.clone();

or

int pCopy1[] = new int[player.length];
System.arraycopy(player, 0, pCopy1, 0, player.length);
0
 
CEHJCommented:
>>DefaultMultableNode thisOne = new DefaultMutableNode(int[] anArray); ??  Would this work?

If you change DefaultMutableNode to DefaultMutableTreeNode, yes
0
 
jtcyAuthor Commented:
Ooops....typo. I mean..so....thisOne.anArray[watever] would work?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CEHJCommented:
If 'watever' is a primitive, no. If it's an object, yes. If it is a primitive, you could create a wrapper
0
 
zzynxSoftware engineerCommented:
Java help says:

public DefaultMutableTreeNode(Object userObject)
Creates a tree node with no parent, no children, but which allows children, and initializes it with the specified user object.

Parameters:
userObject - an Object provided by the user that constitutes the node's data

You can get your object back via getUserObject()

java help says:

public Object getUserObject()
Returns this node's user object.

Returns: the Object stored at this node by the user
0
 
zzynxSoftware engineerCommented:
The user object can be whatever Object you want: array, list, map, ...
You name it as long as it is an Object
0
 
objectsCommented:
>>>DefaultMultableNode thisOne = new DefaultMutableNode(int[] anArray); ??  Would this work?
>If you change DefaultMutableNode to DefaultMutableTreeNode, yes

Even if u change it to DefaultMutableTreeNode it still won't work.

The following will work if anArray is an an array of integers.

DefaultMultableNode thisOne = new DefaultMutableNode(anArray);
0
 
CEHJCommented:
>>Even if u change it to DefaultMutableTreeNode it still won't work.

It'll work fine, as the following demonstrates:

            int[] ints = { 1, 2, 3 };
            javax.swing.tree.DefaultMutableTreeNode node = new javax.swing.tree.DefaultMutableTreeNode(ints);
            System.out.println(((int[])node.getUserObject())[1]);
0
 
objectsCommented:
Yes that will work, which is the same as the line of code I posted.

0
 
objectsCommented:
I of course left out the change to class name :)

DefaultMultableTreeNode thisOne = new DefaultMutableTreeNode(anArray);

0
 
CEHJCommented:
>>Yes that will work, which is the same as the line of code I posted.

So why did you post this then?

>>>>
>>>DefaultMultableNode thisOne = new DefaultMutableNode(int[] anArray); ??  Would this work?
>If you change DefaultMutableNode to DefaultMutableTreeNode, yes

Even if u change it to DefaultMutableTreeNode it still won't work.
>>>>

or do you imagine that jtcy means that literally, rather than means 'can i pass an int array to the node constructor?' ;-)

?
0
 
objectsCommented:
i meant what i said, that that line won't work.
0
 
CEHJCommented:
LOL - you obviously do think he meant that then
0
 
jtcyAuthor Commented:
I have imported javax.swing.tree.*;

why this still doesnt work?

      public void minimax(DefaultMultableTreeNode aNode)
        {
       .....

it says cant resolve symbol DefaultMultableTreeNode
0
 
objectsCommented:
u spelt it wrong:
DefaultMutableTreeNode
0
 
CEHJConnect With a Mentor Commented:
Still a typo, that's why

If you change DefaultMultableTreeNode to DefaultMutableTreeNode it will work
0
 
jtcyAuthor Commented:
DefaultMutableTreeNode root = new DefaultMutableTreeNode(pPos); // passing array pPos (int)
minimax(root);

public void minimax(DefaultMutableTreeNode aNode)
 {
     // creating nodes...
     int pCopy1[] = new int[aNode.player.length]; // I want to make a copy of the array i just passed in.....doesnt work?
                        
0
 
objectsCommented:
int[] player = (int[]) aNode.getUserObject();
int pCopy1[] = new int[player.length];
0
 
jtcyAuthor Commented:
oh..then i wont need to do the second line then since it already clone one at the first line.

int[] player = (int[]) aNode.getUserObject();
0
 
objectsCommented:
the first line justr gets a reference to the existing array, it does not create a new array.
0
 
jtcyAuthor Commented:
I don understand.

int[] player = (int[]) aNode.getUserObject();   // is player here you mean new array created in minimax method?
int pCopy1[] = new int[player.length];           // would pCopy1[] holds content of aNode's array?

0
 
objectsCommented:
int[] player = (int[]) aNode.getUserObject();   // is player here you mean new array created in minimax method?

yes

int pCopy1[] = new int[player.length];           // would pCopy1[] holds content of aNode's array?

No it is an empty array the same size as player.
You would use System.arraycopy() to copy the array.
0
 
objectsCommented:
Do u actually need a copy of the array? Or do you just need to access the array?
0
 
jtcyAuthor Commented:
I need to copy the array. I need to create 4 clones of it cuz I need to change each of their contents but don wan to modify the original.
0
 
jtcyAuthor Commented:
// cant this work?

int[] pcopy = (int[]) aNode.getUserObject().clone();
0
 
jtcyAuthor Commented:
i think you don get what i mean.

i pass the node which contains the array to the method. I need to IN the method, create an array which is a clone of the passed in node's array.

0
 
objectsCommented:
yes thats what the above code does.
0
 
jtcyAuthor Commented:
but in the method, the compiler cant recognize what is player
0
 
objectsCommented:
have you added the line:

int[] player = (int[]) aNode.getUserObject();  
0
 
jtcyAuthor Commented:
oh, in that case why cant int[] player = (int[]) aNode.getUserObject();   be enough? so u mean if i just use this line, int[] player is a reference to the original and modifying it could modify the original?
0
 
objectsCommented:
> in that case why cant int[] player = (int[]) aNode.getUserObject();   be enough?

cause you said u wanted a *copy*.

> so u mean if i just use this line, int[] player is a reference to the original and modifying it could modify the original?

correct
0
 
jtcyAuthor Commented:
perfect
0
 
objectsCommented:
0
 
CEHJCommented:
8-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.