【问题标题】:Saving a Integer[] as an array将 Integer[] 保存为数组
【发布时间】:2015-11-01 21:45:02
【问题描述】:

我想弄清楚如何将Integer[]{longest, count, possible}方法的返回值保存为局部变量,并使用局部变量作为返回值。

我遇到的一个问题是,当我编译我的代码时,我收到一个错误,即 int 无法转换为布尔值,它指的是 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};   
    }

这是我的 lengthOfColor 方法

private Integer[] lengthOfColor(int col, int row, int pattern, boolean color) {
        int x = 0;
        int y = 0;
        if (pattern == 1) {
            // vertical pattern
            y = 1;
        } else if (pattern == 2) {
            // horizontal pattern
            x = 1;
        } else if (pattern == 3) {
            // diagonal slope left pattern
            x = 1;
            y = 1;
        } else {
            // diagonal slope right pattern
            x = 1;
            y = -1;
        }
        // length = how many neighbor slots are of same color
        // possible equals number of slots, that you can play off of.
        // whichSide = left or right if horizontal and top or bottom if vertical.
        int length = 0;
        int possible = 0;
        Integer[] whichSide = new Integer[]{1, -1}; 
        for (int side : whichSide) {
            int i = 1;
            boolean complete = false;
            //while complete is false continue the loop
            while (!complete) {
                //mainX == horizontal pattern distance
                //mainY == vertical pattern distance
                int mainX = x * i * side;
                int mainY = y * i * side;
                //if still inbounds and if the same slot is filled and it matches the color, increment length
                if (!outOfBounds(col, row, mainX, mainY) && getIsFilled(col, row, mainX, mainY) &&
                    checkColor(col, row, mainX, mainY) == color) 
                {
                    length++;
                } 
                //if still inbounds and if the same slot is empty, increment possible number of spots and change complete to true
                else if (!outOfBounds(col, row, mainX, mainY) && !getIsFilled(col, row, mainX, mainY) && 
                           getLowestEmptyIndex(myGame.getColumn(col + mainX)) == getLowestEmptyIndex(myGame.getColumn(col)) + mainY - row) 
                {
                    possible++;
                    complete = true;
                }
                //finish the statement to avoid a infinite loop if neither conditions are met.
                else
                {
                    complete = true;


     }
            // If not complete, then check one slot further.
            i = i + 1;
        }
    }
    return new Integer[] {length, possible};
}

【问题讨论】:

  • 在哪一行出现错误。
  • 我怀疑问题实际上来自您的lengthOfColor 方法。

标签: java arrays methods return


【解决方案1】:

想通了,我想得很辛苦。我只需要在返回之前保存整数,以便以后更容易访问并且有一个名称。

Integer[] longestLengthArray = new Integer[]{longest, count, possible};
        return longestLengthArray; 

【讨论】:

    【解决方案2】:

    最简单的解决方案:

    return new Integer[]{longest, count, possible? 1 : 0}; 
    

    但总的来说,创建新的辅助类是个好主意:

    class LongestLengthRersponse{
         private int longest;
         private int count;
         private boolean possible;
         //constructor, and getters
    

    【讨论】:

    • 可能是int 而不是boolean
    • 正如 codebender 评论的那样,可能是一个 int,而 1 : 0 到底是什么意思
    • @Brentonr25 查找“三元运算符”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2017-03-11
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2013-10-18
    相关资源
    最近更新 更多