arraylist - Update Tree node using pass by value in Java -


Before I show my problem which I understand: uses java from near pricing.

Under the example, I refer to the "my_root" reference, node (),

and "NodeModify (my_root)" in the row; The compiler is expected to pass the copy of the "my_root" memory address and "root = new_node;" inside the function I.E = & gt; Hopefully the memory location / address of the root will be updated at the location / location where "new_node" is located.

Surprisingly, it does not reflect back from the main () function, from where it was called ???

  package com.trees; Public class nodeemption {public static zero node modid (node ​​root) {System.out.println ("[nodmodivate] is the latest root:" + root); Node new_node = new node (0); System.out.println ("[node] is the new node:" + new_node); Root = new_node; System.out.println ("[NodeModify] is root:" + root); } Public static zero main (string [] arg) {node my_root = new node (55); System.out.println ("My Root is Before node Edit:" + my_root); NodeModify (my_root); System.out.println ("Node is my root after the edit:" + my_root); }}   

However, if I do this with other references, such as Arreif, it updates in main () as I expected! Example:

  package com.trees; Import java.util.ArrayList; Public category array update class {public static zero updateMarialistlist (arrelisted & lt; string & gt; sample) {sample.add ("was updated!"); } Public static zero main (string [] args) {ArrayList & lt; String & gt; My_list = New Arrestist & lt; String & gt; (); My_list.add ("My List"); System.out.println ("** Before the call list: **"); PrintArrayList (my_list); UpdateMyArrayList (my_list); System.out.println ("\ n ** after call list: **"); PrintArrayList (my_list); } Private Static void printArrayList (ArrayList & lt; string & gt; my_list) {for (string item: my_list) {System.out.println (item); }}}   

What I'm missing here, I had a hard time updating the root node in creating binary trees! Please help! Thank you.

Passing in the context of a method means the reference is a value. This value is made available to you in the name of the method parameter root , which stays on the stack. Assigning different references to the parameter root will only appear within the method, but as soon as you leave the method, everything is lost on the stack.

What you want to achieve is a modification of the value hold by variable my_root To do this, you want to do this in the References variable my_root < / Code> and not only value my_root . This can be done, for example, by passing in the container, which is in the context of the node s, a Java archive is one of those containers to pass in the container and To modify its values, edit the node (node ​​root) .

Some side notes:

  • A method must be started in Java with lower-case letters or underscores
  • Use camels for variable names instead of underscores for different words, when you define constants < / Div>

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -