“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:-1
我得到了一个错误:“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:-1,java,arrays,exception,Java,Arrays,Exception,我得到了一个错误: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at SnakeBox.neighbor(SnakeBox.java:150) at SnakeBox.findSnake(SnakeBox.java:86) at pg6a.main(pg6a.java:28) 正在使用的类是: /** This class can be used to manipulate 2D boxe
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at SnakeBox.neighbor(SnakeBox.java:150)
at SnakeBox.findSnake(SnakeBox.java:86)
at pg6a.main(pg6a.java:28)
正在使用的类是:
/** This class can be used to manipulate 2D boxes of "snakes" for pg6.
*/
import java.util.*;
import java.io.*;
public class SnakeBox {
// instance variables
private char[][] box;
private int rows, columns;
private int snakeCount;
private int startRow, startCol;
private int endRow, endCol;
private boolean finish;
private int x, y;
private String toDisplay;
/** Create and initialize a SnakeBox by reading a file.
@param filename the external name of a plain text file
*/
public SnakeBox(String filename) throws IOException{
Scanner filescan = new Scanner(new FileReader(filename));
rows = filescan.nextInt();
columns = filescan.nextInt();
snakeCount = filescan.nextInt();
filescan.nextLine();
box = new char[rows][columns];
for (int i=0; i < rows; i++) {
String line = filescan.nextLine();
for (int j=0; j < columns; j++) {
char character = line.charAt(j);
box[i][j] = character;
}
}
}
/** Create a new snakebox of a given size and snake count
@param rows the height of the box
@param cols the number of columns
@param snakes how many snakes are in the box
*/
public SnakeBox(int rows, int cols, int snakes) {
this.rows = rows;
this.columns = cols;
this.snakeCount = snakes;
}
/** Display the box on the screen.
*/
public void display() {
String toDisplay = "";
for (int row = 0; row < rows; row++) {
for (int column = 0; column < columns; column++) {
toDisplay += box[row][column];
}
toDisplay += '\n';
}
System.out.print(toDisplay);
}
/** Find the next snake, skinning to change it from S to .
*/
public void findSnake() {
// check to make sure there are still snakes to skin
if (finish = true) {
int row, col, nb;
// find an S to search from
outerloop:
for (int k=0; k < rows; k++) {
for (int l=0; l < columns; l++) {
if (box[k][l] == 'S') {
startRow = k;
startCol = l;
endRow = k;
endRow = l;
break outerloop;
}
}
}
// set your initial S position to both start and end
// search from end, updating it as you go
do {
nb = neighbor(endRow, endCol);
switch (nb) {
case 1: endRow--; endCol--;
break;
case 2: endRow--;
break;
case 3: endRow--; endCol++;
break;
case 4: endCol--;
break;
case 5: endCol++;
break;
case 6: endCol--; endRow++;
break;
case 7: endRow++;
break;
case 8: endRow++; endCol++;
break;
}
} while (nb != 0);
// search from start, updating it as you go
do {
nb = neighbor(startRow, startCol);
switch (nb) {
case 1: startRow--; startCol--;
break;
case 2: startRow--;
break;
case 3: startRow--; startCol++;
break;
case 4: startCol--;
break;
case 5: startCol++;
break;
case 6: startCol--; startRow++;
break;
case 7: startRow++;
break;
case 8: startRow++; startCol++;
break;
}
} while (nb != 0);
// update snake count
snakeCount = snakeCount - 1;
//display start/end points of the snake, then display it
}
/** Change a position from S to . and find a neighboring S if one exists.
@param x the row number of the position to change
@param y the column number of the position to change
@return 0 if no S neighbor was found, or a number from this grid
indicating the position of the found neighbor
1 2 3
4 S 5
6 7 8
*/
}
private int neighbor(int x, int y) {
box[x][y] = '.';
if (box[x--][y--] == 'S')
return 1;
if (box[x--][y] == 'S')
return 2;
if (box[x--][y++] == 'S')
return 3;
if (box[x][y--] == 'S')
return 4;
if (box[x][y++] == 'S')
return 5;
if (box[x++][y--] == 'S')
return 6;
if (box[x++][y] == 'S')
return 7;
if (box[x++][y++] == 'S')
return 8;
else
return 0;
}
/** Display the endpoints of the snake most recently skinned.
*/
public void printEnds() {
System.out.print("(" + x + "," + y + ")");
}
/** Find out how many full snakes are in the box.
@return the snake count
*/
public int getCount() {
return snakeCount;
}
/** Check whether all the snakes have been skinned, based on the snake count.
@return true if done, false otherwise
*/
public boolean finished() {
if (snakeCount == 0) {
finish = true;
}
else {
finish = false;
}
return finish;
}
}
在尝试访问框的边界条件之前,您没有检查框的边界条件。另外,使用x--在每个阶段更改x的值。绝对使用它们-x-1
等
您的其他变量也遇到同样的问题案例2:endRow--代码>
在尝试访问框的边界条件之前,您没有检查框的边界条件。另外,使用x--在每个阶段更改x的值。绝对使用它们-x-1
等
您的其他变量也遇到同样的问题案例2:endRow--
这是因为邻居()
中的那些if
语句
如果(框[x-->[y-->=='S')
-无论其计算结果是真还是假,这都将减少x
&y
类似地,所有if
语句将保持递减
或递增
x&y
。这就是为什么在某一点上,x
或y
的值或两者都低于0
,从而在数组中给出索引-1
。因此出现了ArrayIndexOutOfBoundsException
您可以通过在if
语句中使用x-1
和y+1
来修复它,而不是使用递增/递减
运算符
这一行SnakeBox.neighbor(SnakeBox.java:150)
表示neighbor
方法中存在错误,只需查看neighbor()
方法中的if
语句,就可以告诉您可能出了什么问题。这是因为neighbor()中的if
语句
如果(框[x-->[y-->=='S')
-无论其计算结果是真还是假,这都将减少x
&y
类似地,所有if
语句将保持递减
或递增
x&y
。这就是为什么在某一点上,x
或y
的值或两者都低于0
,从而在数组中给出索引-1
。因此出现了ArrayIndexOutOfBoundsException
您可以通过在if
语句中使用x-1
和y+1
来修复它,而不是使用递增/递减
运算符
这一行SnakeBox.neighbor(SnakeBox.java:150)
说明了neighbor
方法中存在错误,只需查看neighbor()
方法中的if
语句,就可以告诉您可能出现了什么错误。如果您添加在代码中发现错误的方式,效果会更好(提示:可能正在阅读stacktrace?)在使用递增/递减运算符之前,我使用了x-1和y+1,但得到了相同的错误消息。我使用x-1和y-1更新了邻居方法,但仍然得到相同的错误。@Absolute初学者-也就是说,您还必须确保您处理了x&y
为0
的情况。如果您不处理,无论您使用的是x--还是x-1
,都会得到该异常,因为它会给出结果-1
。如果您添加在代码中发现错误的方式(提示:可能会读取堆栈跟踪?)在使用递增/递减运算符之前,我使用了x-1和y+1,但得到了相同的错误消息。我使用x-1和y-1更新了邻居方法,但仍然得到相同的错误。@Absolute初学者-也就是说,您还必须确保您处理了x&y
为0
的情况。如果您不处理,无论您使用的是x--还是x-1
,您都会得到该异常,因为它会给出结果-1
。错误涉及以下行:nb=neighbor(endRow,endCol);if(box[x--][y]='S')返回2;以及来自驱动程序的snakes.findSnake();错误涉及以下行:nb=neighbor(endRow,endCol);if(box[x--][y] =='S')返回2;并从驱动程序返回:snakes.findSnake();在尝试访问框之前,如何检查框的边界条件?如果(y<0 | | y>=box[x].length)这将告诉您是否正在访问超出范围的数组索引。请使用类似的代码检查x的边界。在尝试访问框之前,如何检查框的边界条件?如果(y<0 | | y>=框[x]。长度)这将告诉您是否正在访问超出范围的数组索引。请使用类似代码检查x的边界。
import java.util.*;
import java.io.*;
public class pg6a {
public static void main(String[] args) throws IOException {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the name of the file where your snake box resides, and we will try our very best to eliminate all the snakes in your snake box!");
String filename;
filename = keyboard.nextLine();
SnakeBox snakes = new SnakeBox(filename);
snakes.display();
while (snakes.finished() == false) {
snakes.findSnake();
snakes.printEnds();
snakes.display();
System.out.print(snakes.getCount());
}
}
}
box[x--][y--]