Sha*_*shi 2 c# java arrays int
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]);
}
}
Run Code Online (Sandbox Code Playgroud)
我必须将此c#程序转换为Java语言。但是这行使我感到困惑
pArray = new int [5] {-3,-1,-2,-3,-4}; //此更改是本地的。
如何重新初始化java int数组?感谢帮助。
pArray = new int[] {-3, -1, -2, -3, -4};
Run Code Online (Sandbox Code Playgroud)
即,无需指定初始大小-编译器可以计算大括号内的项目。
另外,请记住,随着java通过值传递,您的数组将不会“更改”。您必须返回新数组。