Redian新闻
>
how to return multiple values of basic data type?
avatar
how to return multiple values of basic data type?# Java - 爪哇娇娃
b*d
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.
avatar
f*n
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 的大作中提到】
: 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.

avatar
n*k
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 的大作中提到】
: 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.

avatar
b*d
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 的大作中提到】
:
: 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;

avatar
f*t
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 的大作中提到】
: 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;

avatar
m*t
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 的大作中提到】
: 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;

avatar
m*c
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 的大作中提到】
:
: 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

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。