-
Passing an un-dimensioned array to a method
How do I pass an array to a method, where the method set the dimension and
populates the array. I would like to do something like:
int x[];
changeValue(x);
System.out.Println(""+x[0]);
System.out.Println(""+x[1]);
public void changeValue(int x){
x[]=new int[2];
x[0]=1;
x[1]=2;
}
Similar to the VB way of ReDim an array. Is this at all possible in Java?
Cheers,
Joakim
-
Re: Passing an un-dimensioned array to a method
"Joakim" <jukke@telia.com> wrote:
>
>How do I pass an array to a method, where the method set the dimension and
>populates the array. I would like to do something like:
>
>int x[];
>changeValue(x);
>System.out.Println(""+x[0]);
>System.out.Println(""+x[1]);
>
>public void changeValue(int x){
> x[]=new int[2];
> x[0]=1;
> x[1]=2;
>}
>
>Similar to the VB way of ReDim an array. Is this at all possible in Java?
>
>Cheers,
>Joakim
Joakim,
In Java, objects are passed by reference and primitives are passed by value.
This means that a primitive values such as int, double, boolean, etc. are
passed into methods by passing a copy of the value. If that value is changed
within the method, only the the copy changes, not the original. Objects
on the other hand are passed by reference. If you make changes via the reference,
then the original object gets changed no matter what method the reference
is changed from. Arrays are objects. If you pass a reference into a method
and make changes via the reference, then the original object will be affected.
Here is an example:
public static void main( String[] args )
{
int x[];
changeValue(x);
System.out.Println(""+x[0]);
System.out.Println(""+x[1]);
}
public static void changeValue( int[] x )
{
x[0]=1;
x[1]=2;
}
If you change the reference so that it refers to a different object, then
the original reference will still point to the original object:
public static void changeValue( int[] x )
{
//This creates a new object, and changes the local reference
x[] = new int[2];
x[0] = 1;
x[1] = 2;
}
This code would change the local reference (which is a copy of the original
reference) by assigning it to a new object. The original reference in the
main() method would remain the same however.
Let me know if you have any more questions.
Happy Coding!
Cordially,
Kyle Gabhart
DevX Java Pro
-
Re: Passing an un-dimensioned array to a method
You have the method return the new array. So instead of "public void
changeValue(int[] x)" you have "public int[] changeValue(int[] x)". Inside
your method you create the new array and return it.
PC2
Joakim <jukke@telia.com> wrote in message news:3aa121e6$1@news.devx.com...
>
> How do I pass an array to a method, where the method set the dimension and
> populates the array. I would like to do something like:
>
> int x[];
> changeValue(x);
> System.out.Println(""+x[0]);
> System.out.Println(""+x[1]);
>
> public void changeValue(int x){
> x[]=new int[2];
> x[0]=1;
> x[1]=2;
> }
>
> Similar to the VB way of ReDim an array. Is this at all possible in Java?
>
> Cheers,
> Joakim
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|