189 |
|
* @param minCapacity the desired minimum capacity |
190 |
|
*/ |
191 |
|
private void growArray(int minCapacity) { |
192 |
+ |
if (minCapacity < 0) throw new OutOfMemoryError(); // int overflow |
193 |
|
int oldCapacity = elementData.length; |
194 |
|
// Double size if small; else grow by 50% |
195 |
|
int newCapacity = ((oldCapacity < 64)? |
196 |
|
(oldCapacity * 2): |
197 |
< |
((oldCapacity * 3)/2 + 1)); |
197 |
> |
((oldCapacity * 3)/2)); |
198 |
|
if (newCapacity < minCapacity) |
199 |
|
newCapacity = minCapacity; |
200 |
|
elementData = Arrays.copyOf(elementData, newCapacity); |
344 |
|
// Positional Access Operations |
345 |
|
|
346 |
|
/** |
347 |
< |
* Creates and returns an appropriate exception for indexing errors. |
347 |
> |
* Throws an appropriate exception for indexing errors. |
348 |
|
*/ |
349 |
< |
private static IndexOutOfBoundsException rangeException(int i, int s) { |
350 |
< |
return new IndexOutOfBoundsException("Index: " + i + ", Size: " + s); |
349 |
> |
private static void rangeException(int i, int s) { |
350 |
> |
throw new IndexOutOfBoundsException("Index: " + i + ", Size: " + s); |
351 |
|
} |
352 |
|
|
353 |
|
/** |
359 |
|
*/ |
360 |
|
public E get(int index) { |
361 |
|
if (index >= size) |
362 |
< |
throw rangeException(index, size); |
362 |
> |
rangeException(index, size); |
363 |
|
return (E)elementData[index]; |
364 |
|
} |
365 |
|
|
374 |
|
*/ |
375 |
|
public E set(int index, E element) { |
376 |
|
if (index >= size) |
377 |
< |
throw rangeException(index, size); |
377 |
> |
rangeException(index, size); |
378 |
|
|
379 |
|
E oldValue = (E) elementData[index]; |
380 |
|
elementData[index] = element; |
389 |
|
*/ |
390 |
|
public boolean add(E e) { |
391 |
|
++modCount; |
392 |
< |
int s = size++; |
392 |
> |
int s = size; |
393 |
|
if (s >= elementData.length) |
394 |
|
growArray(s + 1); |
395 |
|
elementData[s] = e; |
396 |
+ |
size = s + 1; |
397 |
|
return true; |
398 |
|
} |
399 |
|
|
409 |
|
public void add(int index, E element) { |
410 |
|
int s = size; |
411 |
|
if (index > s || index < 0) |
412 |
< |
throw rangeException(index, s); |
412 |
> |
rangeException(index, s); |
413 |
|
++modCount; |
412 |
– |
size = s + 1; |
414 |
|
if (s >= elementData.length) |
415 |
|
growArray(s + 1); |
416 |
< |
System.arraycopy(elementData, index, elementData, index + 1, |
417 |
< |
s - index); |
416 |
> |
System.arraycopy(elementData, index, |
417 |
> |
elementData, index + 1, s - index); |
418 |
|
elementData[index] = element; |
419 |
+ |
size = s + 1; |
420 |
|
} |
421 |
|
|
422 |
|
/** |
430 |
|
*/ |
431 |
|
public E remove(int index) { |
432 |
|
int s = size - 1; |
433 |
< |
if (index > s) |
434 |
< |
throw rangeException(index, size); |
433 |
< |
size = s; |
433 |
> |
if (index < 0 || index > s) |
434 |
> |
rangeException(index, size); |
435 |
|
modCount++; |
436 |
< |
Object oldValue = elementData[index]; |
436 |
> |
E oldValue = (E)elementData[index]; |
437 |
|
int numMoved = s - index; |
438 |
|
if (numMoved > 0) |
439 |
< |
System.arraycopy(elementData, index+1, elementData, index, |
440 |
< |
numMoved); |
439 |
> |
System.arraycopy(elementData, index + 1, |
440 |
> |
elementData, index, numMoved); |
441 |
|
elementData[s] = null; // forget removed element |
442 |
< |
return (E)oldValue; |
442 |
> |
size = s; |
443 |
> |
return oldValue; |
444 |
|
} |
445 |
|
|
446 |
|
/** |