由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - how to return multiple values of basic data type?
相关主题
问个primitive type的问题help "java.lang.NoSuchMethodError"
[转载] Java 1.5 Generic 问题新手问个java蠢问题!
使用object tag如何让IE和firefox兼容applet?在一个函数里把arraylist设为null 但是有问题
高手麻烦帮帮忙。。新手学编程白痴求助
what's inside an java object?线程问题。
How to know the size of a java object ?请教一个简单的问题
Re: SWING中如何实现TEXTLISTENER简单问题
help on this scope question请大牛们贴一些java的面试题好吗
相关话题的讨论汇总
话题: int话题: objects话题: change话题: multiple话题: values
进入Java版参与讨论
1 (共1页)
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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
:
: 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 的大作中提到】
: 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 的大作中提到】
: 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 的大作中提到】
:
: 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

1 (共1页)
进入Java版参与讨论
相关主题
请大牛们贴一些java的面试题好吗what's inside an java object?
Most common Java interview questions I have been asked.How to know the size of a java object ?
interestingRe: SWING中如何实现TEXTLISTENER
Java练习题 5help on this scope question
问个primitive type的问题help "java.lang.NoSuchMethodError"
[转载] Java 1.5 Generic 问题新手问个java蠢问题!
使用object tag如何让IE和firefox兼容applet?在一个函数里把arraylist设为null 但是有问题
高手麻烦帮帮忙。。新手学编程白痴求助
相关话题的讨论汇总
话题: int话题: objects话题: change话题: multiple话题: values