i****1 发帖数: 445 | 1 public void insert (int value) {
rInsert (value, root);
}
private void rInsert (int value, BSTNode node) {
if (node == null) {
node = new BSTNode (value);
} else if (value < node.value) {
rInsert (value, node.left);
} else if (value > node.value) {
rInsert (value, node.right);
}
}
This approach will work in some programming
languages ... but not Java.
不知为为何上面的程序错了?我看挺好的,为何会出错。
而且答案是java里rinsert()函数需要返回值,如private BSTNode rInsert (int
value
, BSTNode node).
这时为何? |
w***o 发帖数: 109 | 2 好像你没有把新的Node和BST链起来。你至少应该有象这样的操作:root.left = (
newly created node) or root.right = (newly created node)
好像任何Programming language都不应该work。你能说那个可以work吗? |
x**********l 发帖数: 271 | 3 java pass argument by value, not reference
【在 i****1 的大作中提到】 : public void insert (int value) { : rInsert (value, root); : } : private void rInsert (int value, BSTNode node) { : if (node == null) { : node = new BSTNode (value); : } else if (value < node.value) { : rInsert (value, node.left); : } else if (value > node.value) { : rInsert (value, node.right);
|
i********m 发帖数: 332 | 4 For every object that is not a primitive type, it's passed by reference.
Only the primitive type and their wrapper are passed by value.
for example :
public void foo (Integer i) {
i = 9;
}
public static void main (String[] args) {
Integer i = new Integer (10);
foo(i);
System.out.println(i);
//The value is still 10 in i, not 9
} |
i****1 发帖数: 445 | 5 可是答案是要求函数增加一个返回值。其他的没变
【在 i********m 的大作中提到】 : For every object that is not a primitive type, it's passed by reference. : Only the primitive type and their wrapper are passed by value. : for example : : public void foo (Integer i) { : i = 9; : } : public static void main (String[] args) { : Integer i = new Integer (10); : foo(i); : System.out.println(i);
|
g********s 发帖数: 125 | 6
【在 i********m 的大作中提到】 : For every object that is not a primitive type, it's passed by reference. : Only the primitive type and their wrapper are passed by value. : for example : : public void foo (Integer i) { : i = 9; : } : public static void main (String[] args) { : Integer i = new Integer (10); : foo(i); : System.out.println(i);
|
g********s 发帖数: 125 | 7 java是pass by value no matter what. 我现在本科 刚学没多久 所以印象深刻。
【在 i********m 的大作中提到】 : For every object that is not a primitive type, it's passed by reference. : Only the primitive type and their wrapper are passed by value. : for example : : public void foo (Integer i) { : i = 9; : } : public static void main (String[] args) { : Integer i = new Integer (10); : foo(i); : System.out.println(i);
|