any way to pass an int parameter as an one element int[]?

H

hyena

Hi,

Just wondering if there exists such a ways that I can pass an "int" value
as an one element "int[]" array to a function in java? Some thing similiar
to:

int a = 0;
foo(&a);

foo(int[] A){...}//not sure if the syntax is right or not, did not use
C++ for long time

in C++.

thanks!

Sun
 
J

Jan =?ISO-8859-1?Q?Thom=E4?=

Well,

if you use varargs syntax you can do it like this:

void foo( int .. A ) {
}

int a = 0;
foo( a );

Will put A automagically into an array. If you cannot use varargs (<1.5)
then you gotta do:

void foo( int [] A ) {

}

int a = 0;

foo ( new int [] {a} );

Greetings,
Jan
Hi,

Just wondering if there exists such a ways that I can pass an "int"
value
as an one element "int[]" array to a function in java? Some thing similiar
to:

int a = 0;
foo(&a);

foo(int[] A){...}//not sure if the syntax is right or not, did not
use
C++ for long time

in C++.

thanks!

Sun
 
H

hyena

Thanks Jan. The second one is the answer to me. I am using 1.4.2.

Jan Thomä said:
Well,

if you use varargs syntax you can do it like this:

void foo( int .. A ) {
}

int a = 0;
foo( a );

Will put A automagically into an array. If you cannot use varargs (<1.5)
then you gotta do:

void foo( int [] A ) {

}

int a = 0;

foo ( new int [] {a} );

Greetings,
Jan
Hi,

Just wondering if there exists such a ways that I can pass an "int"
value
as an one element "int[]" array to a function in java? Some thing
similiar
to:

int a = 0;
foo(&a);

foo(int[] A){...}//not sure if the syntax is right or not, did not
use
C++ for long time

in C++.

thanks!

Sun
 
T

Thomas Schodt

Jan said:
void foo( int [] A ) {

}

int a = 0;

foo ( new int [] {a} );


However, changing A[0] inside foo() has no effect on a.


public class Pass {
public static void main(String[] arg) {
new Pass().run();
}
void run() {
int a = 5;
foo(new int[]{a});
System.out.println(a);
}
void foo(int[] A) {
A[0] = 7;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top