gpt4 book ai didi

java - 在 Java 中将数组保存为局部变量

转载 作者:太空宇宙 更新时间:2023-11-04 13:33:56 28 4
gpt4 key购买 nike

所以我需要找到一种方法将数组保存为局部变量并在方法末尾返回它。我有点困惑,我被告知除了我自己尝试过的之外,我还有另外两个选择。我可以将数组存储为变量或使用哈希表。我想将数组保存为局部变量。

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

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

最佳答案

// int col; boolean color; int row;
Integer[] result;
result = longestLength(col, color, row);

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

关于java - 在 Java 中将数组保存为局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31890447/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com