Click to See Complete Forum and Search --> : Passing array by ref
NOMADMAN
Mar 11th, 2003, 02:09 PM
I have a main method and two other public methods in my main class.
main makes an array of default constructors. (ie int [] mainArray = new int[100];) Then a function call to fxnA passes the array (ie fxnA( mainArray );) In fxnA takes an array on integers (ie. fxnA( int arr[] );) fxnA passes the array to another fxn, fxnB (ie fxnB( arr );). And fxn Bs parameters look like this: fxnB( int a[] ); From this B does some stuff. then returns it to A which returns it to main, full complete and specilized.
When I run the main class it returns an array of 0's (default ints) to main from fxnA but in fxnA I outputed the array and found its to be correct there, so I assume B and A are doing its job. Why did it do this?
I was thought arrays where passed by reference in java?
NOMAD
Dillinger4
Mar 11th, 2003, 03:11 PM
Yeah i get the same results. The array being returned is unaffected by and changes made to it by any methods. Even when the array returned is assigned to another array. :confused:
class C {
public static void main(String args[]){
int[] i = {1,2,3,4,5};
for(int x = 0; x < i.length; ++x){
System.out.println(i[x]);
}
int[] i2 = squares(i);
for(int x = 0; x < i2.length; ++x){
System.out.println(i2[x]);
}
}
public static int[] squares(int[] i){
for(int x = 0; x < i.length; ++x){
System.out.println(i[x] * 2);
}
return i;
}
}
Dillinger4
Mar 11th, 2003, 03:16 PM
Here is what i dug up. If an actual parameter is a reference to an object, the the reference value of the object is passed. This means that bolth the actual parameter and the formal parameter are aliases to the same object during the invocation of the method. In particular, this implies that changes made to the object via the formal parameter will be apparent after the call returns but then they go on to say..... In summary, the formal parameter can only change the state of the object whose reference value was passed to the method. The parameter passing strategy in Java is call-by-value and not call-by-reference regardless of the type of parameter. These seem to be two contradicting statements. :confused:
NOMADMAN
Mar 11th, 2003, 05:28 PM
Yes they do, I fixed my immediate problem by making an temp array in fxn a that took b and then assigned it to array. This seemed to work, but I may be having another similar problem.
I'm coding a QuickSort and using Suns example: http://java.sun.com/applets/jdk/1.0/demo/SortDemo/QSortAlgorithm.java
Its not the same thing I guess you never pass more than 1 deep. So its not related to the reference chain.
Thanks for your help Dilenger4
NOMAD
NOMADMAN
Mar 12th, 2003, 12:19 AM
Still no good, someone else have an answer? Tell me if Im on the right track.
Passing by reference passes an objects point so if I send it twice does it like not dereference it and sends the pointers pointer? It can't that would be counter intuitive and probably a compiler error.
I've search Sun with no more info that Dilenger4.
NOMAD
NOMADMAN
Mar 12th, 2003, 02:32 AM
import java.lang.*;
public class ArrayTest
{
public static void main (String args[])
{
int [] mainArray = new int[10];
System.out.print("main(pre)::mainArray: ");
for (int j=0; j<mainArray.length; ++j)
System.out.print(mainArray[j] + " ");
System.out.println("");
fxnA( mainArray );
System.out.print("main(post)::mainArray: ");
for (int i=0; i<mainArray.length; ++i)
System.out.print(mainArray[i] + " ");
System.out.println("");
}
public static void fxnA( int [] arr )
{
System.out.print("fxnA(pre)::arr: ");
for (int i=0; i<arr.length; ++i)
System.out.print(arr[i] + " ");
System.out.println("");
for (int h=0; h<arr.length; ++h)
arr[h] = h;
fxnB( arr );
System.out.print("fxnA(post)::arr: ");
for (int j=0; j<arr.length; ++j)
System.out.print(arr[j] + " ");
System.out.println("");
}
public static void fxnB( int [] a )
{
System.out.print("fxnB(pre)::a: ");
for (int i=0; i<a.length; ++i)
System.out.print(a[i] + " ");
System.out.println("");
for (int h=0; h<a.length; ++h)
a[h] += a.length;
System.out.print("fxnB(post)::a: ");
for (int j=0; j<a.length; ++j)
System.out.print(a[j] + " ");
System.out.println("");
}
}
I found this to work, I'm confused. I guess there was something wrong in my code.
NOMAD
abhijit
Mar 18th, 2003, 10:16 PM
Originally posted by Dilenger4
Yeah i get the same results. The array being returned is unaffected by and changes made to it by any methods. Even when the array returned is assigned to another array. :confused:
class C {
public static void main(String args[]){
int[] i = {1,2,3,4,5};
for(int x = 0; x < i.length; ++x){
System.out.println(i[x]);
}
int[] i2 = squares(i);
for(int x = 0; x < i2.length; ++x){
System.out.println(i2[x]);
}
}
public static int[] squares(int[] i){
for(int x = 0; x < i.length; ++x){
System.out.println(i[x] * 2);
}
return i;
}
}
class C {
public static void main(String args[]){
int[] i = {1,2,3,4,5};
for(int x = 0; x < i.length; ++x){
System.out.println(i[x]);
}
int[] i2 = squares(i);
for(int x = 0; x < i2.length; ++x){
System.out.println(i2[x]);
}
}
public static int[] squares(int[] i){
for(int x = 0; x < i.length; ++x){
i[x] = i[x] * 2; //See this line. That has made the difference.
System.out.println(i[x] * 2);
}
return i;
}
}
I fixed your earlier example and the array is definitely getting changed. You were outputting the changes, but not assigning anything to the array.
Cheers!
Abhijit
Dillinger4
Mar 18th, 2003, 10:57 PM
Ahh ok. I thought i was starting to go crazy. :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.