【问题标题】:Saving an array as a local variable in Java在 Java 中将数组保存为局部变量
【发布时间】:2015-08-08 06:43:21
【问题描述】:

所以我需要想办法将数组保存为局部变量并在方法结束时返回它。我有点困惑,有人告诉我,除了我自己尝试过的之外,我还有另外两个选择。我可以将数组存储为变量或使用哈希表。我想将数组保存为局部变量。

下面是我用来尝试将返回值保存为局部变量数组的方法。

private Integer[] longestLength(int col, boolean color, int row) {
    // longest equals length of the longest route
    // count equals number of the longest routes
    // possible equals number of spots, that you can play off of.
    int longest = 0;
    int count = 0;
    int possible = 0;
    //this for loop counts to 4, the patterns of the 4 possible wins
    for (int i = 1; i <= 4; i++) {
        int length = lengthOfColor(col, color, i, row)[0];
        //if a new longest is found, its new value is now set to itself
        if (length > longest) {
            longest = length;
            count = 0;
            possible = lengthOfColor(col, color, i, row)[1];
        }
        //if length is the same as longest, we increase the count, and make possible equal too the larger one
        if (longest != 0 && length == longest) {
            count++;
            possible = Math.max(lengthOfColor(col, color, i, row)[1], possible);
        }
    }
    return new Integer[]{longest, count, possible};
}

【问题讨论】:

  • 这有什么问题?
  • 在方法内声明一个数组,在其中存储任何你想要的,然后在方法结束时返回

标签: java arrays methods


【解决方案1】:
// int col; boolean color; int row;
Integer[] result;
result = longestLength(col, color, row);

局部变量result 将保存longestLength() 的返回值。

【讨论】:

    【解决方案2】:

    从您目前所展示的内容来看,您不需要使用装箱的原始 Integer。以下是您可以执行的两个版本。

    Integer[] myIntegerArray = longestLength(col, color, row);
    

    更好的方法是将您的方法更改为 private int[] longestLength(int col, boolean color, int row) 并让它返回 new int[]{longest, count, possible};

    int[] myIntArray = longestLength(col, color, row);
    

    两者之间的区别在于 Integer 与 int 的开销较小。因此,当您不需要 Integer 时,使用 int 会比 Integer 更快。

    【讨论】:

      【解决方案3】:

      你可以这样做:

      private Integer[] longestLength(int col, boolean color, int row) {
          Integer array[]=new Integer[size]; // make an array
          // Do your work
          .............
      
          return array; // return the array
      } 
      

      【讨论】:

        【解决方案4】:

        看起来很简单,我的理解是您需要创建一个本地数组,该数组可以保存代码中显示的方法中的值。

        您只需要在您的案例中创建一个具有相同数据类型的本地数组(如以下代码中提到的“longestRouteCount”Integer),然后调用将返回值分配给本地数组变量的方法。

        Integer longestRouteCount [];
        longestRouteCount = longestLength(longest, count, possible);
        

        如果您有相同的疑问,请分享,否则请详细说明。 如果您认为此答案对您有所帮助,请投票。

        【讨论】:

          【解决方案5】:

          就像你刚才说的,你的实际代码没有问题。

          但是如果你想使用一个局部变量,你只需要在你的方法中声明一个新数组,把这些值放在上面,然后返回它,所以在你的方法中执行以下操作。

          // declare a local array in your method
          Integer[] output = new Integer[3]();
          
          // put the values you need inside your array
          output[0] = new Integer(longest);
          output[1] = new Integer(count);
          output[2] = new Integer(possible;);
          
          // finally return it
          return output;
          

          注意:

          当您将它们添加到此数组时,请记住将您的值转换为 Integer,因为它们的类型为 int

          【讨论】:

          • @Brentonr25 这个答案对你有帮助吗?如果是,请接受。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多