今天在写算法题的时候,本来想调用 List 的 set(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:
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
相应的代码如下:

解决方法:
Replaces the element at the specified position in this list with the specified element (optional operation).
1
这是 List 中对于 set 方法的注释,明确指出,该方法是用来替换某个索引位置的元素值的,并不是用来新增元素的。新增元素最好还是用 add 方法。
截图中我最后是用普通数组来代替 ArrayList ,因为大小我已经知道了,最后再调用 Arrays.asList 方法转为List 的子类。
