【问题标题】:How to reinitialize the int array in java如何在java中重新初始化int数组
【发布时间】:2010-12-23 07:19:34
【问题描述】:
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 数组?感谢帮助。

【问题讨论】:

    标签: c# java arrays int


    【解决方案1】:

    如果你想在java中改变大小,你可能需要使用Vector,或者ArrayList

    【讨论】:

      【解决方案2】:

      这是 C# 程序打印的内容:

      **在Main里面,在调用方法之前,第一个元素是:1

      在方法内部,第一个元素是:-3

      Main里面,调用方法后,第一个元素是:888**

      问问自己,为什么在调用 Change() 之后 Main() 中的 arr[0] 设置为 888?你期待 -3 吗?

      这就是正在发生的事情。 int 数组变量 pArrayChange() 方法中被视为局部变量。它最初设置为对传递给它的数组实例的引用。 (在示例程序中,这将是 Main() 中的 arr)。线

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

      导致创建一个新数组,pArray 被设置为对这个新数组的引用,而不是 Main() 中的 arr

      程序没有打印出数组长度。如果有,长度将分别为 3、5 和 3。

      您可以尝试以下方法:

      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]);
          }
      }
      

      【讨论】:

        【解决方案3】:

        正如您正确指出的,这是 不可能 使用 Java 参数传递语义(C# 具有这些场景的 ref 关键字)。

        由于 Java 数组大小不可变,您只能更改值,而不能更改数组的长度(它不能增长也不能缩小)。

        【讨论】:

        • 这没有抓住重点,因为有问题的那一行并没有改变数组,它分配了一个新数组。
        【解决方案4】:

        您不能在其他方法中“重新初始化”数组,因为 Java 是按值传递的。您可以使用 ref 关键字在 C# 中解决此问题,但这在 Java 中不可用。您将只能通过调用方法更改现有数组中的元素。

        如果您只想在本地更改数组,那么 Bozho 的解决方案将起作用。

        【讨论】:

          【解决方案5】:

          当存在数组初始化器时,您无法提供维度,即

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

          【讨论】:

            【解决方案6】:
            pArray = new int[] {-3, -1, -2, -3, -4};
            

            即,无需指定初始大小 - 编译器可以计算大括号内的项目。

            另外,请记住,当 java 按值传递时,您的数组不会“改变”。您必须返回新数组。

            【讨论】:

            • Java 是按值传递的。方法返回时不会反映这一点。
            • 在你问下一个问题之前 - “为什么它没有改变” - 检查我更新的答案和 Taylor 的答案。
            • 问题没有具体说明,但是程序显然没有运行,因为出现了编译时错误。
            • 哦,对不起!!!。我拼错了你的名字。谢谢 Bozho.. 我只想问一个问题,如果我必须在不传递初始化程序的情况下为这个 int 数组分配新的大小。有可能吗?
            • 只要说 pArray = new int[5];
            猜你喜欢
            • 2012-11-10
            • 2017-06-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-01-19
            • 2021-01-31
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多