make ArrayList.set() do bounds checking

This commit is contained in:
Joel Dice 2007-11-15 11:53:33 -07:00
parent a6a1f8ba98
commit 4f047ded8c

View File

@ -108,12 +108,13 @@ public class ArrayList<T> implements List<T> {
}
public T set(int index, T element) {
if (index >= size) {
resize(index+1);
if (index >= 0 && index < size) {
Object oldValue = array[index];
array[index] = element;
return (T) oldValue;
} else {
throw new IndexOutOfBoundsException(index + " not in [0, " + size + ")");
}
Object oldValue = array[index];
array[index] = element;
return (T) oldValue;
}
public T remove(int index) {