为什么在这里我得到了IndexOutofBoundsException 16并且Eclipse指向了行中的以下代码:
if((temp[0][0] == '.' && temp[0][height - 1] == '.') ||如果我传递以下字符串:
String layout =
"..aa\n"+
"..aa\n"+
"...a\n"+
"...a\n"+
"...a\n"+
"...a\n"+
"...a\n"+
"aaaa\n"+
"aaaa\n"+
"...a\n"+
"...a\n"+
"...a\n"+
"...a\n"+
"...a\n"+
"...a\n"+
"...a\n"+
"...a\n"+
""; 在这个方法中,我得到了那个错误。请smb帮我一下好吗?我非常肯定,我做的条件后,loops.NOTE:**Layout strings** 的形状必须至少有一个填充块在每个,0th column**,** last row**,和** last column**.**,谢谢!
public static Shape makeShape(String layout,char displayChar)
{
Shape result;
int height = 0;
int width = 0;
Scanner data = new Scanner(layout);
while(data.hasNextLine())
{
String line = data.nextLine();
width = line.length();
height++;
}
char[][] temp = new char[height][width];
Scanner data2 = new Scanner(layout);
for(int i = 0; i < height; i++)
{
String line = data2.nextLine();
for(int j = 0; j < width; j++)
{
if(line.charAt(j) != '.')
temp[i][j] = displayChar;
if(line.charAt(j) == '.')
temp[i][j] = line.charAt(j);
}
}
data2.close();
if((temp[0][0] == '.' && temp[0][height - 1] == '.') ||
(temp[height - 1][0] == '.' && temp[height - 1][width - 1] == '.'))
throw new FitItException("Empty borders!");
result = new CreateShape(height, width, displayChar, temp);
return result;
}发布于 2015-04-25 23:35:09
在temp[height - 1][width - 1]中,第一个索引是高度,第二个查找是宽度。现在,如果你把这些弄糊涂了,用高度来表示宽度,而高度大于宽度,它会爆炸。
也许你想让temp[0][width -1]
发布于 2015-04-25 23:35:40
仔细观察
temp[0][height - 1]在第二维度中使用height
char[][] temp = new char[height][width];但是在这里,你的height是第一维的,width是第二维的。
https://stackoverflow.com/questions/29871852
复制相似问题