tags:

views:

367

answers:

6
class PassingRefByVal 
{
    static void Change(int[] pArray)
    {
        pArray[0] = 888;  // This change affects the original element.
        pArray = new int[5] {-3, -1, -2, -3, -4};   // This change is local.
        System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]);
    }

    static void Main() 
    {
        int[] arr = {1, 4, 5};
        System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr [0]);

        Change(arr);
        System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr [0]);
    }
}

I have to convert this c# program into java language. But this line confuse me

pArray = new int[5] {-3, -1, -2, -3, -4}; // This change is local.

How can i reinitialize the java int array? Thanks for help.

+1  A: 
pArray = new int[] {-3, -1, -2, -3, -4};

i.e., no need to specify the initial size - the compiler can count the items inside the curly brackets.

Also, have in mind that as java passed by value, your array won't 'change'. You have to return the new array.

Bozho
Java is pass by value. This won't be reflected when the method returns.
Taylor Leese
It works. Thanks bonzo.
Shashi Bhushan
you are right, but this is another part of the answer.
Bozho
The question did not specifiy *what* was causing confusion...
Michael Borgwardt
before you ask your next question - 'why it doesn't change' - check my updated answer, and Taylor's answer.
Bozho
the question did not specify, but the program obviously didn't run, because there was a compile-time error.
Bozho
oh sorry!!!. I misspelled your name. Thanks Bozho.. Just i want ask to one question, if i have to assign new size to this int array without passing initializer. is it possible?
Shashi Bhushan
just say pArray = new int[5];
Taylor Leese
+2  A: 

You can't "reinitialize" the array from within the other method because Java is pass by value. You can solve this in C# by using the ref keyword, but this is not available in Java. You will only be able to change the elements in the existing array from the calling method.

If you only want the array to be changed locally then Bozho's solution will work.

Taylor Leese
A: 

You can't supply dimensions when an array initalizer is present i.e.

 pArray = new int[5] {-3, -1, -2, -3, -4};
Gordon
A: 

as you correctly noted, this is impossibile with the Java parameter passing semantic (C# has the ref keywords for these scenarios).

Since Java arrays are immutable in size you can change only the values, not the array's lenght (it cannot grown nor shrink).

dfa
This misses the point, because that line in question doesn't change an array, it assigns a new array.
Svante
+1  A: 

Here is what the C# program prints:

**Inside Main, before calling the method, the first element is: 1

Inside the method, the first element is: -3

Inside Main, after calling the method, the first element is: 888**

Ask yourself, why is arr[0] set to 888 in Main() after the call to Change()? Were you expecting to -3?

Here is what is going on. The int array variable pArray is treated as a local variable inside the Change() method. It is initially set to be a reference to the array instance that is passed to it. (In the example program, this would be arr in Main()). The line

**pArray = new int[5] { -3, -1, -2, -3, -4 };   // This change is local.**

causes a new array to be created, and pArray is set to be a reference to this new array instead of the arr from Main().

The program did not print the array lengths out. If it had, the lengths would have been 3, 5, and 3 respectively.

You could try the following:

public class TestPassByRefByVal
{
    public static void Change(int[] pArray)
    {
        int [] lArray = { -3, -1, -2, -3, -4 };
        pArray[0] = 888;  // This change affects the original element.
        pArray = lArray;     // This change is local.
        System.out.println("Inside the method, the first element is: " + pArray[0]);
    }

    public static void main(String[]args)
    {
        int [] arr = { 1, 4, 5 };
        System.out.println("Inside Main, before Change(), arr[0]: " + arr[0]);

        Change(arr);
        System.out.println("Inside Main,  after Change(), arr[0]: " + arr[0]);
    }
}
Jay Elston
A: 

If you want to change the size in java, you might need to use Vector, or ArrayList

Steve Zhang