英文:
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("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");
}
}
答案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 if
s should be else if
s. 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 < 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);
}
}
// ... print the matrix (skipped for brevity) ...
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] + " is a peak");
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论