IndexOutofBounds异常在查找2D数组中的峰值时发生

huangapple 未分类评论44阅读模式
英文:

IndexOutofBounds Exception when finding a peak in 2d arrays

问题

我已经清楚地定义了所有的限制条件,但我仍然不断收到索引超出范围的异常。有人可以看看告诉我可能出错的地方吗?我正在尝试在一个二维数组中找到多个峰值。谢谢。

Scanner o = new Scanner(System.in);

System.out.println("Enter length of array: ");
int row = o.nextInt();
System.out.println("A 2d array of " + row + "x" + row + " has been generated");

int[][] table = new int[row][row];

for (int i = 0; i < row; i++){
    for (int j = 0; j < row; j++){
        table[i][j] = (int)(Math.random() * 100);
    }
}

for (int i = 0; i < row; i++){
    for (int j = 0; j < row; j++){
        System.out.print(table[i][j] + "\t");
    }
    System.out.println("");
}
int n = row - 1;
for (int a = 0; a < row; a++){
    for (int b = 0; b < row; b++){
        if (a == 0 &&  b == 0 && table[a][b] > table[a+1][b] && table[a][b] > table[a][b+1]){
            System.out.println(table[a][b] + " is a peak");
        }
        else if (a == 0 && table[a][b] > table[a][b-1] && table[a][b] > table[a+1][b] && table[a][b] > table[a][b+1]){
            System.out.println(table[a][b] + " is a peak");
        }
        if (b == 0 && table[a][b] > table[a-1][b] && table[a][b] > table[a+1][b] && table[a][b] > table[a][b+1]){
            System.out.println(table[a][b] + " is a peak");
        }
        if (table[a][b] > table[a][b-1] && table[a][b] > table[a-1][b] && table[a][b] > table[a][b+1] && table[a][b] > table[a+1][b]){
            System.out.println(table[a][b] + " is a peak");
        }
        if (a == n && b == n && table[a][b] > table[a-1][b] && table[a][b] > table[a][b-1]){
            System.out.println(table[a][b] + " is a peak");
        }
        if (a == n && table[a][b] > table[a][b+1] && table[a][b] > table[a-1][b] && table[a][b] > table[a][b-1]){
            System.out.println(table[a][b] + " is a peak");
        }
        if (b == n && table[a][b] > table[a+1][b] && table[a][b] > table[a-1][b] && table[a][b] > table[a][b-1]){
            System.out.println(table[a][b] + " is a peak");
        }
    }
}
英文:

I've clearly defined all the limits, yet I keep getting index out of bounds exception. Can anyone have a look and tell me where I could be wrong. I'm trying to find multiple peaks in a 2d array. Thanks.

Scanner o = new Scanner(System.in);
    
    System.out.println(&quot;Enter length of array: &quot;);
    int row = o.nextInt();
    System.out.println(&quot;A 2d array of &quot; + row + &quot;x&quot; + row + &quot; has been generated&quot;);
    
    int[][] table = new int[row][row];

    
    for (int i = 0; i &lt; row; i++){
        for (int j = 0; j &lt; row; j++){
            table[i][j] = (int)(Math.random() * 100);
        }
    }
    
    for (int i = 0; i &lt; row; i++){
        for (int j = 0; j &lt; row; j++){
            System.out.print(table[i][j] + &quot;\t&quot;);
        }
        System.out.println(&quot;&quot;);
    }
    int n = row - 1;
    for (int a = 0; a &lt; row; a++){
        for (int b = 0; b &lt; row; b++){
            if (a == 0 &amp;&amp;  b == 0 &amp;&amp; table[a][b] &gt; table[a+1][b] &amp;&amp; table[a][b] &gt; table[a][b+1]){
                System.out.println(table[a][b] + &quot; is a peak&quot;);
            }
            else if (a == 0 &amp;&amp; table[a][b] &gt; table[a][b-1] &amp;&amp; table[a][b] &gt; table[a+1][b] &amp;&amp; table[a][b] &gt; table[a][b+1]){
                System.out.println(table[a][b] + &quot; is a peak&quot;);
            }
            if (b == 0 &amp;&amp; table[a][b] &gt; table[a-1][b] &amp;&amp; table[a][b] &gt; table[a+1][b] &amp;&amp; table[a][b] &gt; table[a][b+1]){
                System.out.println(table[a][b] + &quot; is a peak&quot;);
            }
            if (table[a][b] &gt; table[a][b-1] &amp;&amp; table[a][b] &gt; table[a-1][b] &amp;&amp; table[a][b] &gt; table[a][b+1] &amp;&amp; table[a][b] &gt; table[a+1][b]){
                System.out.println(table[a][b] + &quot; is a peak&quot;);
            }
            if (a == n &amp;&amp; b == n &amp;&amp; table[a][b] &gt; table[a-1][b] &amp;&amp; table[a][b] &gt; table[a][b-1]){
                System.out.println(table[a][b] + &quot; is a peak&quot;);
            }
            if (a == n &amp;&amp; table[a][b] &gt; table[a][b+1] &amp;&amp; table[a][b] &gt; table[a-1][b] &amp;&amp; table[a][b] &gt; table[a][b-1]){
                System.out.println(table[a][b] + &quot; is a peak&quot;);
            }
            if (b == n &amp;&amp; table[a][b] &gt; table[a+1][b] &amp;&amp; table[a][b] &gt; table[a-1][b] &amp;&amp; table[a][b] &gt; table[a][b-1]){
                System.out.println(table[a][b] + &quot; is a peak&quot;);
            }
        }

答案1

得分: 0

看起来第三到第七个 if 应该改成 else if。考虑情况 a=b=0:第一个 if 被执行,第二个被跳过,但第三个再次执行并因为 a-1 不是有效行而报错。顺便提一下,对于给定元素,这也会多次打印消息。

如果你的目标是找到严格大于其(最多)四个相邻元素的元素,我建议在矩阵周围填充非常大的值。这将摆脱所有的检查并且大大缩短代码。类似这样:

int[][] table = new int[row + 2][row + 2];

for (int i = 0; i < row + 2; i++) {
  table[0][i] = table[row + 1][i] = 100;
  table[i][0] = table[i][row + 1] = 100;
}

for (int i = 1; i <= row; i++) {
  for (int j = 1; j <= row; j++) {
    table[i][j] = (int)(Math.random() * 100);
  }
}

// ... 打印矩阵(为了简洁起见跳过)...

for (int i = 1; i <= row; i++) {
  for (int j = 1; j <= row; j++) {
    if (table[i][j] > table[i][j - 1] &&
        table[i][j] > table[i - 1][j] &&
        table[i][j] > table[i][j + 1] &&
        table[i][j] > table[i + 1][j]) {
      System.out.println(table[i][j] + " 是一个峰值");
  }
}
英文:

It looks like the the third through seventh ifs should be else ifs. Consider the case a=b=0: The first if is executed, the second one is skipped, but the third one is executed again and gives an error as a-1 is not a valid row. As a side note, this can also print the message several times for a given element.

If your aim is to find elements that are strictly larger than their (up to) four neighbors, may I suggest padding the matrix with very large values? That will get rid of all the checks and shorten the code considerably. Something like:

int[][] table = new int[row + 2][row + 2];

for (int i = 0; i &lt; row + 2; i++) {
  table[0][i] = table[row + 1][i] = 100;
  table[i][0] = table[i][row + 1] = 100;
}

for (int i = 1; i &lt;= row; i++) {
  for (int j = 1; j &lt;= row; j++) {
    table[i][j] = (int)(Math.random() * 100);
  }
}

// ... print the matrix (skipped for brevity) ...

for (int i = 1; i &lt;= row; i++) {
  for (int j = 1; j &lt;= row; j++) {
    if (table[i][j] &gt; table[i][j - 1] &amp;&amp;
        table[i][j] &gt; table[i - 1][j] &amp;&amp;
        table[i][j] &gt; table[i][j + 1] &amp;&amp;
        table[i][j] &gt; table[i + 1][j]) {
      System.out.println(table[i][j] + &quot; is a peak&quot;);
  }
}

huangapple
  • 本文由 发表于 2020年4月5日 20:25:43
  • 转载请务必保留本文链接:https://java.coder-hub.com/61042548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定