b****d 发帖数: 246 | 1 for example, I have a function as follows:
void f(int[][] a, int[] b){
a = ...
b = ...
}
However, when it returns, a and b are not changed.
Is there any way to change multiple values without
using objects?
Thanks. | f****n 发帖数: 208 | 2 Java does not allow pointers. So if you want to change the value of multiple
varibles, make them the properties of an object and pass the object as
parameter for the function. I don't know what else can we do.
【在 b****d 的大作中提到】![](/moin_static193/solenoid/img/up.png) : for example, I have a function as follows: : void f(int[][] a, int[] b){ : : a = ... : b = ... : } : However, when it returns, a and b are not changed. : Is there any way to change multiple values without : using objects? : Thanks.
| n*****k 发帖数: 123 | 3
What do you mean it can not be changed.
The primary type arrays are also objects, objects are passing by the copy the
address(or reference). If programming correctly, the value of a[] definitely
can be changed.
for example:
int a[]=new int[]{1,2,3};
void f(int[] b){
b[1]=3;
b[2]=4;
}
Calling f() can definitely change the value of the a[]. of course, if you let
b refer to a new array inside the f() such as b=new int[]{};, then it is a
totally different story.
Actually, passing a int value by
【在 b****d 的大作中提到】![](/moin_static193/solenoid/img/up.png) : for example, I have a function as follows: : void f(int[][] a, int[] b){ : : a = ... : b = ... : } : However, when it returns, a and b are not changed. : Is there any way to change multiple values without : using objects? : Thanks.
| b****d 发帖数: 246 | 4 I don't intend to change the value of elements in an array. What I want is
to change the address of an array. Codes as follows. It doesn't work because
a and b are still null when f() returns. It can be solved by call two
functions, one return a, the other return b, but it's not neat.
The question Is there any way to change both a and b in one function call
besides using objects?
==================================
void main(args){
int[][] a = null;
int[] b = null;
f(a, b);
}
void f(int[][] a, in
【在 n*****k 的大作中提到】![](/moin_static193/solenoid/img/up.png) : : What do you mean it can not be changed. : The primary type arrays are also objects, objects are passing by the copy the : address(or reference). If programming correctly, the value of a[] definitely : can be changed. : for example: : int a[]=new int[]{1,2,3}; : void f(int[] b){ : b[1]=3; : b[2]=4;
| f**t 发帖数: 27 | 5
because
---------- -------
you can not declare the two varible with the same name,
doing that will cause f() has it's own local varible called a and b,
shadowing the original declared a and b in main(), so a & b in main will
not changed after f().
Instead using f(int[][] anotherA, int [] anotherB) may solve the problem.
the
definitely
【在 b****d 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I don't intend to change the value of elements in an array. What I want is : to change the address of an array. Codes as follows. It doesn't work because : a and b are still null when f() returns. It can be solved by call two : functions, one return a, the other return b, but it's not neat. : The question Is there any way to change both a and b in one function call : besides using objects? : ================================== : void main(args){ : int[][] a = null; : int[] b = null;
| m******t 发帖数: 2416 | 6
You can pass in an Object[] which has two elements - one is int[][],
the other int[]. In the method you can then simply allocate those
(you obviously have to know which type to cast each of them to in the
method)
I would *not* do that, though. It's simply bad code.
【在 b****d 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I don't intend to change the value of elements in an array. What I want is : to change the address of an array. Codes as follows. It doesn't work because : a and b are still null when f() returns. It can be solved by call two : functions, one return a, the other return b, but it's not neat. : The question Is there any way to change both a and b in one function call : besides using objects? : ================================== : void main(args){ : int[][] a = null; : int[] b = null;
| m**c 发帖数: 90 | 7
No, the reason is because (someone has mentioned already): objects are
passing by the copy the address(or reference). Addresses (or references) of
"a" and "b" didn't change before and after calling "f(..)".
copy
【在 f**t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : : because : ---------- ------- : you can not declare the two varible with the same name, : doing that will cause f() has it's own local varible called a and b, : shadowing the original declared a and b in main(), so a & b in main will : not changed after f(). : Instead using f(int[][] anotherA, int [] anotherB) may solve the problem. : the : definitely
|
|