Solved

DefaultMultableNode

Posted on 2004-04-14
34
246 Views
Last Modified: 2008-02-01
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?
0
Comment
Question by:jtcy
  • 14
  • 11
  • 7
  • +1
34 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10823600
>>DefaultMultableNode thisOne = new DefaultMutableNode(int[] anArray); ??  Would this work?

If you change DefaultMutableNode to DefaultMutableTreeNode, yes
0
 

Author Comment

by:jtcy
ID: 10823624
Ooops....typo. I mean..so....thisOne.anArray[watever] would work?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10823751
If 'watever' is a primitive, no. If it's an object, yes. If it is a primitive, you could create a wrapper
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 37

Expert Comment

by:zzynx
ID: 10823881
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10823901
The user object can be whatever Object you want: array, list, map, ...
You name it as long as it is an Object
0
 
LVL 92

Expert Comment

by:objects
ID: 10827987
>>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 10828048
>>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
 
LVL 92

Expert Comment

by:objects
ID: 10828087
Yes that will work, which is the same as the line of code I posted.

0
 
LVL 92

Expert Comment

by:objects
ID: 10828094
I of course left out the change to class name :)

DefaultMultableTreeNode thisOne = new DefaultMutableTreeNode(anArray);

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10828142
>>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
 
LVL 92

Expert Comment

by:objects
ID: 10828157
i meant what i said, that that line won't work.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10828191
LOL - you obviously do think he meant that then
0
 

Author Comment

by:jtcy
ID: 10829619
I have imported javax.swing.tree.*;

why this still doesnt work?

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

it says cant resolve symbol DefaultMultableTreeNode
0
 
LVL 92

Expert Comment

by:objects
ID: 10829623
u spelt it wrong:
DefaultMutableTreeNode
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 20 total points
ID: 10829624
Still a typo, that's why

If you change DefaultMultableTreeNode to DefaultMutableTreeNode it will work
0
 

Author Comment

by:jtcy
ID: 10829645
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
 
LVL 92

Expert Comment

by:objects
ID: 10829665
int[] player = (int[]) aNode.getUserObject();
int pCopy1[] = new int[player.length];
0
 

Author Comment

by:jtcy
ID: 10829674
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
 
LVL 92

Expert Comment

by:objects
ID: 10829681
the first line justr gets a reference to the existing array, it does not create a new array.
0
 

Author Comment

by:jtcy
ID: 10829688
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
 
LVL 92

Expert Comment

by:objects
ID: 10829700
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
 
LVL 92

Expert Comment

by:objects
ID: 10829704
Do u actually need a copy of the array? Or do you just need to access the array?
0
 

Author Comment

by:jtcy
ID: 10829709
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
 
LVL 92

Accepted Solution

by:
objects earned 40 total points
ID: 10829724
int[] pcopy = (int[]) player.clone();

or

int pCopy1[] = new int[player.length];
System.arraycopy(player, 0, pCopy1, 0, player.length);
0
 

Author Comment

by:jtcy
ID: 10829744
// cant this work?

int[] pcopy = (int[]) aNode.getUserObject().clone();
0
 

Author Comment

by:jtcy
ID: 10829748
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
 
LVL 92

Expert Comment

by:objects
ID: 10829763
yes thats what the above code does.
0
 

Author Comment

by:jtcy
ID: 10829771
but in the method, the compiler cant recognize what is player
0
 
LVL 92

Expert Comment

by:objects
ID: 10829775
have you added the line:

int[] player = (int[]) aNode.getUserObject();  
0
 

Author Comment

by:jtcy
ID: 10829779
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
 
LVL 92

Expert Comment

by:objects
ID: 10829796
> 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
 

Author Comment

by:jtcy
ID: 10829798
perfect
0
 
LVL 92

Expert Comment

by:objects
ID: 10830600
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10837185
8-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
javap bin 2 41
xampp tool 12 56
jdbc error in jsp application 20 63
String array comparison 4 38
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question