Results 1 to 8 of 8

Thread: Passing array by ref

  1. #1

    Thread Starter
    Addicted Member NOMADMAN's Avatar
    Join Date
    Aug 2002
    Location
    Closer than you think
    Posts
    237

    Passing array by ref

    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

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.
    Code:
     
    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; 
      }
     }

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.

  4. #4

    Thread Starter
    Addicted Member NOMADMAN's Avatar
    Join Date
    Aug 2002
    Location
    Closer than you think
    Posts
    237
    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/...Algorithm.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

  5. #5

    Thread Starter
    Addicted Member NOMADMAN's Avatar
    Join Date
    Aug 2002
    Location
    Closer than you think
    Posts
    237

    Angry

    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

  6. #6

    Thread Starter
    Addicted Member NOMADMAN's Avatar
    Join Date
    Aug 2002
    Location
    Closer than you think
    Posts
    237
    Code:
    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

  7. #7
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228
    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.
    Code:
     
    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; 
      }
     }

    Code:
    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

  8. #8

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width