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

Started by
17 comments, last by rip-off 14 years, 1 month ago
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?
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
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]);}
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.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
I'll just rethink my algorithm. Thanks for the reply.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
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.
neverMind
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
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.
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;			}
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
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.
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){}

This topic is closed to new replies.

Advertisement