在使用ArrayList时遇到一个很愚蠢的问题,想在大小为10的ArrayList的第5个位置插入10,结果抛异常。代码示例如下
ArrayList<Integer> arr=new ArrayList<Integer>(10);
arr.add(5, 10);
异常为
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 0
at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:643)
at java.util.ArrayList.add(ArrayList.java:455)
很是郁闷,明明初始化了大小为10的空间,跑出的异常却告诉我size为0。好吧,只能直接点进去看源代码了。版本1.7
public void add(int index, E element) {
rangeCheckForAdd(index);
//添加的时候扩容
ensureCapacityInternal(size + 1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
add方法,从异常看,是rangeCheckForAdd(index);这行,点进去,为rangeCheckForAdd(int index)这个函数
private void rangeCheckForAdd(int index) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
最终抛出去的异常内容来源为:
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}
这就是开始那个异常Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size: 0
接下来只能看Size为啥为0了,源代码:
/**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;
貌似真相大白了,size只是此ArrayList所包含的元素个数,不是它的容量大小(并不是数组的长度)。添加元素成功后会执行size++,删除元素成功会执行size--。
那么怎么办呢?看ArrayList的带参数构造函数,根本就没有对size进行操作赋值 只是指定了数组的大小,所以size == 0。
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
改进:
List<String> list1 = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
list1.add(i+"");
}
System.err.println(list1.toString());
//构造一个空元素的集合
List<String> list2 = new ArrayList<>(Arrays.asList(new String[list1.size()]));
list2.set(8,"2");
System.err.println(list2.toString());