Question

I keep getting this weird issues when I'm constructing my Ulam (Prime) Spiral. What I am doing in SpiralMaker() is using a 4 step method to create the spiral. As long as the location is less than the total squares in the array, and it is on step 1 then it should move over 1 step to the right and place a 2. When it hits step 2, it moves 1 up and places numLocation which is now a 3. On step 3 it moves left twice taking 2 steps and setting those locations as 4 and 5, etc.

But when I'm running this, I get the below exception on line 32,

Exception in thread "main" 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;
    }
}
Était-ce utile?

La solution

It looks like you are running the loop for one too many iterations. Because the array is [5][5] and you are using [r][c+1] you never want c to be 4.

change

for (int c = 0; c < gridCol; c++){

to

for (int c = 0; c < gridCol-1; c++){

and see if that works

Autres conseils

Remember that arrays in Java are zero indexed.

Change:

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

to:

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

You are allocationg an two dimensional array grid[gridRow][gridCol], which is at least a 5x5 array. In line 29 you are counting up from 0 to gridCol (=4). In line 32 you are incrementing gridcol (or c) again. Thus you are indexing the 6th element of the array, however, the array has only 5 elements...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top