【问题标题】:Can't add variables to my array无法将变量添加到我的数组
【发布时间】:2013-04-29 17:31:58
【问题描述】:

我在构建 Ulam (Prime) Spiral 时不断遇到这个奇怪的问题。我在 SpiralMaker() 中所做的是使用 4 步方法来创建螺旋。只要位置小于数组中的总方格,并且它在第 1 步,那么它应该向右移动 1 步并放置 2。当它到达第 2 步时,它会向上移动 1 并放置 numLocation现在是 3。在第 3 步,它向左移动两次,走 2 步并将这些位置设置为 4 和 5,等等。

但是当我运行它时,我在第 32 行得到以下异常,

线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:5

import java.util.Arrays;

public class GridMaker {
    private static int gridRow = 5; // R = length
    private static int gridCol = 5; // C = height
    private static int[][] grid = new int[gridRow][gridCol];
    private static int totalSteps = (gridRow * gridCol); // total blocks on the grid
    private static int numLocation = 1; // location refers to the number in the box, ie. 1, 2, 3, etc.
    private static int rowLength = 1;
    private static int colLength = 1;

    public static void main(String[] args) {
        grid[Calc.findArrayCenter(gridRow)][Calc.findArrayCenter(gridRow)] = 1;
        spiralMaker();

        for (int r = 0; r < gridRow; r++){
            for (int c = 0; c < gridCol; c++){
                System.out.print(grid[r][c] + " ");
            }
            System.out.println("");
        }
    }

    private static void spiralMaker() {
        for (int stepMaker = 0; stepMaker < 4; stepMaker++){//counter for the steps, only needs 1 of these

            if (stepMaker == 1 && numLocation < totalSteps){
                for (int r = 0; r < gridRow; r++){ //starts browsing array for the last number
                    for (int c = 0; c < gridCol; c++){
                        if(grid[r][c] == (numLocation - 0)){ //when it finds the last number
                            for(int i = 0; i < rowLength; i++){
                                grid[r][c + 1] = numLocation + 1; //when 0 no errors, when 1 "java.lang.ArrayIndexOutOfBoundsException"
                            }
                            numLocation++;
                            rowLength++;
                        }
                    }
                }
            }

        }
    }
}

// -----------------------

public class Calc {
    public static int findArrayCenter(int center) {
        int fcenter = 0;
        if (center % 2 != 0)
        fcenter = (int) ((center / 2));
        else
        fcenter = (center / 2);
        return fcenter;
    }

    public static boolean isOdd(int num) {
        boolean result = true;
        if (num % 2 == 0)
        result = false; // false = even, true = odd
        return result;
    }

    public static boolean primeTester(int num) {
        if (num == 1)
        return false;
        if (num % 2 == 0 && num != 2)
        return false;
        for (int primeCheck = 3; primeCheck <= num / 2; primeCheck += 2) {
            if (num % primeCheck == 0 && num != primeCheck)
            return false;
        }
        return true;
    }
}

【问题讨论】:

  • 网格[r][c + 1] = numLocation + 1;又是什么 +1 呢?
  • 它会查找它计数到的最后一个数字的位置,移动超过 1 步并将该数字 + 1
  • static ints 嗯……你为什么要使用它们? jw..

标签: java arrays multidimensional-array indexoutofboundsexception spiral


【解决方案1】:

看起来您正在运行循环进行了太多次迭代。因为数组是 [5][5] 而你使用的是 [r][c+1] 你永远不希望 c 是 4。

改变

for (int c = 0; c

for (int c = 0; c

看看有没有效果

【讨论】:

    【解决方案2】:

    请记住,Java 中的数组是零索引的。

    更改:

    grid[r][c + 1] = numLocation + 1;
    

    收件人:

    grid[r-1][c] = numLocation + 1;
    

    【讨论】:

      【解决方案3】:

      您正在分配一个二维数组 grid[gridRow][gridCol],它至少是一个 5x5 数组。在第 29 行,您从 0 向上计数到 gridCol (=4)。在第 32 行,您将再次增加 gridcol(或 c)。因此,您正在索引数组的第 6 个元素,但是,该数组只有 5 个元素...

      【讨论】:

      • 我这样做了,但现在我无法用这个遍历数组:for(int i = 0; i
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 2012-09-21
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      相关资源
      最近更新 更多