Jump to content
  • Advertisement
Sign in to follow this  
Concentrate

I hate Java pass-by-reference-by-value .

This topic is 3051 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Once again, java pass by reference by value kills me. This code :
public Position<T> insert(T obj) { 
	return _insert(_root,obj);
}
calls this private helper :
/* Helper functions */
private Position<T> _insert(BTreeNode<T> root, T insertValue)
{
	if(root == null){
		root = new BTreeNode<T>(insertValue);
	}
	else
	{
		int result = insertValue.compareTo(root.element());
		
		if(result < 0){ //value < root
			_insert(root.getLeft(),insertValue);
		}
		else if(result > 0){ // value > root
			_insert(root.getRight(),insertValue);
		}
	}
	return root; //either a new leaf or the position with the same data
}
It does not work because I can't change the reference of the root. Any one wanna just throw some ideas around?

Share this post


Link to post
Share on other sites
Advertisement
Well, you can always simulate "pass T by reference" with passing a reference to a one-element array by value...

public static void changeString(String[] array)
{
array[0] = "updated";
}

public static void main(String[] args)
{
String[] array = {"original"};
changeString(array);
System.out.println(array[0]);
}

Share this post


Link to post
Share on other sites
I guess, but thats kind of ugly. I would do an iterative solution, but am required
for a recursive one. Do you think I can add another parameter and use that
somehow Plus I am not allowed to change the _root type.

Share this post


Link to post
Share on other sites
You could return the new root as a BTreeNode, and then assign the returned value to root, left or right where appropriate.

I'm not entirely sure what distinction there is between Position<T> and BTreeNode<T>, but I think something like the above will work. It might be easier still to move the _insert implementation into the BTreeNode<T> type.

Share this post


Link to post
Share on other sites
If you keep the "null root" as the base recursive case, it becomes a little more elegant looking.

Plus that code looks dangerously like it could clear the tree every time a second element is added.

I have a draft implementation, but this looks like homework so I'll refrain from posting it until you get a little closer.

Share this post


Link to post
Share on other sites
Yes this is H.W. I got it to work, however I guess it doesn't look very elegent
as a C++ tree would. Thanks for not posting the code.

public Position<T> insert(T obj) {
if(_root == null){
_root = new BTreeNode<T>(obj);
return _root;
}
else
return _insert(_root,obj);
}
/* Helper functions */
private Position<T> _insert(BTreeNode<T> root, T insertValue)
{
BTreeNode<T> ret = null;

if(root != null )
{ //compare value
int result = insertValue.compareTo(root.element());
//if value < root
if(result < 0){
if(root.getLeft() == null){ //check if we should add it to left
//add newNode to the left
root.setLeft(new BTreeNode<T>(insertValue));
ret = root.getLeft();
}
//else transversal left
else _insert(root.getLeft(),insertValue);
}
//if value > root
else if(result > 0){
//check if we should add it to the right
if(root.getRight() == null){
//add leaf to the right
root.setRight(new BTreeNode<T>(insertValue));
ret = root.getRight();
}
//else move
else _insert(root.getRight(),insertValue);
}
else ret = root; //node with the same data
}
return ret;
}


Share this post


Link to post
Share on other sites
My hint is that if the line "_root = new BTreeNode<T>(obj);" stays inside the helper function _insert(), there are less special cases to handle. My solution is remarkably close to your original attempt.

Share this post


Link to post
Share on other sites
For what it's worth, this is one of the many things about Java that make me hate with a passion reserved for few things in life.

How simple is it in practically every other language in the world to write a function that returns multiple values? For example, suppose you have some item cache and for speed you want to write a TryGet function that if successful returns true as well as the item, and if failed simply returns false? Simple in every language in the world except Java. In Java you have to do something crazy like create a new class just for the return value, like:


public class CacheLookupResult<T>
{
public bool found;
public T item;
}

public CacheLookupResult<T> TryGetItem(int key)
{
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!