Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
hyena - 23 Nov 2006 12:49 GMT
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
Jan Thomä - 23 Nov 2006 12:55 GMT
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,
>
[quoted text clipped - 15 lines]
>
> Sun

Signature

__________________________________________________________
insOMnia - We never sleep...
http://www.insomnia-hq.de

hyena - 23 Nov 2006 13:02 GMT
Thanks Jan. The second one is the answer to me. I am using 1.4.2.

> Well,
>
[quoted text clipped - 40 lines]
>>
>> Sun
Thomas Schodt - 23 Nov 2006 13:48 GMT
> void foo( int [] A ) {
>
[quoted text clipped - 3 lines]
>
> 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;
    }
}
Andreas Leitgeb - 23 Nov 2006 17:14 GMT
>> void foo( int [] A ) {
>> }
>> int a = 0;
>> foo ( new int [] {a} );
>
> However, changing A[0] inside foo() has no effect on a.

to "fix" that:

int a = 0;
int[] ia = {a};
foo (ia);
a=ia[0];


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.