How to reinitialize the int array in java
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.
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.
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.
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]);
}
}
You can't supply dimensions when an array initalizer is present i.e.
pArray = new int[5] {-3, -1, -2, -3, -4};
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).
If you want to change the size in java, you might need to use Vector, or ArrayList
精彩评论