【发布时间】: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};
}
【问题讨论】:
-
这有什么问题?
-
在方法内声明一个数组,在其中存储任何你想要的,然后在方法结束时返回