Removes the object at the specified location from this list. @param location the index of the object to remove. @return the removed object. @throws IndexOutOfBoundsException when location < 0 || >= size()
(int location)
| 469 | * when {@code location < 0 || >= size()} |
| 470 | */ |
| 471 | @Override |
| 472 | public E remove(int location) { |
| 473 | E result; |
| 474 | if (location < 0 || location >= size) { |
| 475 | throw new IndexOutOfBoundsException("Index out of bounds"); |
| 476 | } |
| 477 | if (location == 0) { |
| 478 | result = array[firstIndex]; |
| 479 | array[firstIndex++] = null; |
| 480 | } else if (location == size - 1) { |
| 481 | int lastIndex = firstIndex + size - 1; |
| 482 | result = array[lastIndex]; |
| 483 | array[lastIndex] = null; |
| 484 | } else { |
| 485 | int elementIndex = firstIndex + location; |
| 486 | result = array[elementIndex]; |
| 487 | if (location < size / 2) { |
| 488 | System.arraycopy(array, firstIndex, array, firstIndex + 1, |
| 489 | location); |
| 490 | array[firstIndex++] = null; |
| 491 | } else { |
| 492 | System.arraycopy(array, elementIndex + 1, array, |
| 493 | elementIndex, size - location - 1); |
| 494 | array[firstIndex+size-1] = null; |
| 495 | } |
| 496 | } |
| 497 | size--; |
| 498 | |
| 499 | // REVIEW: we can move this to the first if case since it |
| 500 | // can only occur when size==1 |
| 501 | if (size == 0) { |
| 502 | firstIndex = 0; |
| 503 | } |
| 504 | |
| 505 | modCount++; |
| 506 | return result; |
| 507 | } |
| 508 | |
| 509 | @Override |
| 510 | public boolean remove(Object object) { |