1

當我構建我的Ulam(總理)螺旋時,我不斷收到這個奇怪的問題。我在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; 
    } 
} 
+0

grid [r] [c + 1] = numLocation + 1;什麼是再次的+1? – renz 2013-05-06 00:54:26

+0

它會查找它計算的最後一個數字的位置,移動1步並添加該數字+ 1 – 2013-05-06 01:10:58

+0

'static ints'是meh ...爲什麼要使用它們? jw .. – Tdorno 2013-05-06 02:11:14

回答

1

看起來你正在運行的環路太多了迭代。因爲陣列是[5] [5],並且使用[R] [C + 1]您從不希望c鍵是4

變化

對(INT C = 0; C^< gridCol; C++){

對(INT C = 0; C^< gridCol-1; C++){

,看看是否可行

+0

非常感謝你!這有助於解決我的問題! :) – 2013-05-06 01:13:43

1

請記住,Java中的數組是零索引。

變化:

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

到:

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

您allocationg一個2個dimensiona l array grid [gridRow] [gridCol],它至少是一個5x5的數組。在第29行中,您從0到gridCol(= 4)。在第32行中,您正在增加gridcol(或c)。因此,您正在對數組的第6個元素進行索引,但是,該數組只有5個元素...

+0

我做到了,但現在我無法用這個遍歷數組:for(int i = 0; i 2013-05-06 01:11:22