Java 将整数[]保存为数组
我试图找出如何将方法的返回值Java 将整数[]保存为数组,java,arrays,methods,return,Java,Arrays,Methods,Return,我试图找出如何将方法的返回值Integer[]{longest,count,mably}保存为局部变量,并使用局部变量作为返回值 我遇到的一个问题是,当我编译代码时,我得到一个错误,int不能转换为boolean,它在return语句中引用了“可能” private Integer[] longestLength(int col, int row, boolean color) { // longest equals length of the longest patt
Integer[]{longest,count,mably}
保存为局部变量,并使用局部变量作为返回值
我遇到的一个问题是,当我编译代码时,我得到一个错误,int不能转换为boolean,它在return语句中引用了“可能”
private Integer[] longestLength(int col, int row, boolean color)
{
// longest equals length of the longest pattern of the same color
// count equals number of the longest pattern of the same color
// 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++) {
//lengthOfColor saves the lengthOfColor() method to avoid calling it multiple times throughout longestLength.
Integer[] lengthOfColor = lengthOfColor(col, row, i, color);
int length = lengthOfColor[0];
//if a new longest is found, its new value is now set to itself
if (length > longest) {
longest = length;
count = 0;
possible = lengthOfColor[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[1], possible);
}
}
return new Integer[]{longest, count, possible};
}
最简单的解决方案:
return new Integer[]{longest, count, possible? 1 : 0};
但一般来说,创建新的帮助器类是个好主意:
class LongestLengthRersponse{
private int longest;
private int count;
private boolean possible;
//constructor, and getters
弄明白了,我想努力。我只需要在返回整数之前保存它,这样以后就更容易访问它,并且有一个名称
Integer[] longestLengthArray = new Integer[]{longest, count, possible};
return longestLengthArray;
您在哪一行得到错误。我怀疑问题实际上来自您的
lengthOfColor
方法。可能是int
而不是boolean
。正如codebender评论的那样,可能是int,1:0到底是什么mean@Brentonr25查找“三元运算符”。
Integer[] longestLengthArray = new Integer[]{longest, count, possible};
return longestLengthArray;