首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取IndexOutofBoundsException

获取IndexOutofBoundsException
EN

Stack Overflow用户
提问于 2015-04-25 23:24:25
回答 2查看 74关注 0票数 1

为什么在这里我得到了IndexOutofBoundsException 16并且Eclipse指向了行中的以下代码:

代码语言:javascript
复制
 if((temp[0][0] == '.' && temp[0][height - 1] == '.') ||

如果我传递以下字符串:

代码语言:javascript
复制
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**.**,谢谢!

代码语言:javascript
复制
 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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-25 23:35:09

temp[height - 1][width - 1]中,第一个索引是高度,第二个查找是宽度。现在,如果你把这些弄糊涂了,用高度来表示宽度,而高度大于宽度,它会爆炸。

也许你想让temp[0][width -1]

票数 3
EN

Stack Overflow用户

发布于 2015-04-25 23:35:40

仔细观察

代码语言:javascript
复制
temp[0][height - 1]

在第二维度中使用height

代码语言:javascript
复制
char[][] temp = new char[height][width];

但是在这里,你的height是第一维的,width是第二维的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29871852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档