ListInteger pathnew ArrayList(8);path.set(21);这段代码会出问题吗答案是会的public ArrayList(int initialCapacity) { if (initialCapacity 0) { this.elementData new Object[initialCapacity]; } else if (initialCapacity 0) { this.elementData EMPTY_ELEMENTDATA; } else { throw new IllegalArgumentException(Illegal Capacity: initialCapacity); } }带参数的构造方法如下只是分配了空间但是并没有给size实际元素个数赋值但是在set索引值这个方法的时候会判断当前的下标是不是小于size所以会报错Index 0 out of bounds for length 0set源码public E set(int index, E element) { Objects.checkIndex(index, size); E oldValue elementData(index); elementData[index] element; return oldValue; }--checkIndex调用方法ForceInline public static int checkIndex(int index, int length) { return Preconditions.checkIndex(index, length, null); }-- 调用checkIndexIntrinsicCandidate public static X extends RuntimeException int checkIndex(int index, int length, BiFunctionString, ListNumber, X oobef) { if (index 0 || index length) throw outOfBoundsCheckIndex(oobef, index, length); return index; }