Solved

DefaultMultableNode

Posted on 2004-04-14
34
244 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse IDE - Cannot copy/paste from console output 8 136
Problem to start Neon 20 89
eclipse java  build path 6 45
replace a word with other 1 40
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

864 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

19 Experts available now in Live!

Get 1:1 Help Now