java中如何重新初始化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]);
}
}
我必须将这个 c# 程序转换为 java 语言。但这行让我感到困惑
pArray = new int[5] {-3, -1, -2, -3, -4}; // 此更改是本地的。
如何重新初始化 java 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]);
}
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
即,无需指定初始大小 - 编译器可以计算大括号内的项目。
另外,请记住,当 java 按值传递时,您的数组不会“更改”。您必须返回新数组。
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.
您无法从其他方法中“重新初始化”数组,因为 Java 是按值传递的。您可以在 C# 中使用 ref 关键字来解决此问题,但这在 Java 中不可用。您只能通过调用方法更改现有数组中的元素。
如果您只想在本地更改数组,那么 Bozho 的解决方案将起作用。
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.
下面是 C# 程序打印的内容:
**在 Main 内部,调用方法之前,第一个元素是: 1
在方法内部,第一个元素是:-3
在 Main 内部,调用方法之后,第一个元素是:888* *
问问自己,为什么在调用 Change() 后,Main() 中的 arr[0] 设置为 888?你期待-3吗?
这是正在发生的事情。 int 数组变量 pArray 在 Change() 方法中被视为局部变量。它最初被设置为对传递给它的数组实例的引用。 (在示例程序中,这将是 Main() 中的 arr)。该行
导致创建一个新数组,并且 pArray 被设置为对此新数组的引用,而不是来自 Main() 的 arr。
该程序没有打印出数组长度。如果有,长度将分别为 3、5 和 3。
您可以尝试以下操作:
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
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:
当存在数组初始值设定项时,您无法提供维度,即
You can't supply dimensions when an array initalizer is present i.e.
正如您正确指出的那样,这对于 Java 参数传递语义是不可能(C# 对于这些场景有 ref 关键字)。
由于 Java 数组的大小不可变,您只能更改值,而不能更改数组的长度(它不能增长也不能收缩)。
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).
如果你想在java中改变大小,你可能需要使用Vector,或者ArrayList
If you want to change the size in java, you might need to use Vector, or ArrayList