侧边栏壁纸
博主头像
Lin2J博主等级

升级了服务器,访问应该会更加流畅🇨🇳

  • 累计撰写 99 篇文章
  • 累计创建 43 个标签
  • 累计收到 5 条评论

目 录CONTENT

文章目录

ArrayList调用set方法报越界错误

Lin2J
2021-06-15 / 0 评论 / 0 点赞 / 507 阅读 / 1,127 字 / 正在检测是否收录...

今天在写算法题的时候,本来想调用 Listset(int, Object) 方法,向指定位置新增一个元素,可是却报了数组越界异常,把我给整懵了。

运行失败:
java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
    at line 64, java.base/jdk.internal.util.Preconditions.outOfBounds
    at line 70, java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex
    at line 248, java.base/jdk.internal.util.Preconditions.checkIndex
    at line 373, java.base/java.util.Objects.checkIndex
    at line 440, java.base/java.util.ArrayList.set
    at line 28, Solution.levelOrder
    at line 57, __DriverSolution__.__helper__
    at line 82, __Driver__.main
    测试用例:[3,9,20,null,null,15,7]
stdout:

相应的代码如下:

算法题-数组越界案例

解决方法:

Replaces the element at the specified position in this list with the specified element (optional operation).

这是 List 中对于 set 方法的注释,明确指出,该方法是用来替换某个索引位置的元素值的,并不是用来新增元素的。新增元素最好还是用 add 方法。

截图中我最后是用普通数组来代替 ArrayList ,因为大小我已经知道了,最后再调用 Arrays.asList 方法转为List 的子类。

0

评论区