| 107 | /// |
| 108 | /// - `IndexOutOfBoundsException`: when `location size()` |
| 109 | @Override |
| 110 | public void add(int location, E object) { |
| 111 | if (location < 0 || location > size) { |
| 112 | throw new IndexOutOfBoundsException("" + location + " out of: " + size); |
| 113 | } |
| 114 | if (location == 0) { |
| 115 | if (firstIndex == 0) { |
| 116 | growAtFront(1); |
| 117 | } |
| 118 | array[--firstIndex] = object; |
| 119 | } else if (location == size) { |
| 120 | if (firstIndex + size == array.length) { |
| 121 | growAtEnd(1); |
| 122 | } |
| 123 | array[firstIndex + size] = object; |
| 124 | } else { // must be case: (0 < location && location < size) |
| 125 | if (size == array.length) { |
| 126 | growForInsert(location, 1); |
| 127 | } else if (firstIndex + size == array.length |
| 128 | || (firstIndex > 0 && location < size / 2)) { |
| 129 | System.arraycopy(array, firstIndex, array, --firstIndex, |
| 130 | location); |
| 131 | } else { |
| 132 | int index = location + firstIndex; |
| 133 | System.arraycopy(array, index, array, index + 1, size |
| 134 | - location); |
| 135 | } |
| 136 | array[location + firstIndex] = object; |
| 137 | } |
| 138 | |
| 139 | size++; |
| 140 | modCount++; |
| 141 | } |
| 142 | |
| 143 | /// Adds the specified object at the end of this `ArrayList`. |
| 144 | /// |