Solved

DefaultMultableNode

Posted on 2004-04-14
34
243 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java virtual machine monitoring tools 3 39
changeXy challenge 13 57
pairs challenge 5 44
HSSFWorkbook cannot be resolved error 10 15
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now