Inserts the objects in the specified collection at the specified location in this List. The objects are added in the order they are returned from the collection's iterator. @param location the index at which to insert. @param collection the collection of objects. @return {@cod
(int location, Collection<? extends E> collection)
| 183 | * when {@code location < 0 || > size()} |
| 184 | */ |
| 185 | @Override |
| 186 | public boolean addAll(int location, Collection<? extends E> collection) { |
| 187 | if (location < 0 || location > size) { |
| 188 | throw new IndexOutOfBoundsException("Index out of bounds"); |
| 189 | } |
| 190 | |
| 191 | Object[] dumparray = toObjectArray(collection); |
| 192 | int growSize = dumparray.length; |
| 193 | // REVIEW: Why do this check here rather than check |
| 194 | // collection.size() earlier? RI behaviour? |
| 195 | if (growSize == 0) { |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | if (location == 0) { |
| 200 | growAtFront(growSize); |
| 201 | firstIndex -= growSize; |
| 202 | } else if (location == size) { |
| 203 | if (firstIndex + size > array.length - growSize) { |
| 204 | growAtEnd(growSize); |
| 205 | } |
| 206 | } else { // must be case: (0 < location && location < size) |
| 207 | if (array.length - size < growSize) { |
| 208 | growForInsert(location, growSize); |
| 209 | } else if (firstIndex + size > array.length - growSize |
| 210 | || (firstIndex > 0 && location < size / 2)) { |
| 211 | int newFirst = firstIndex - growSize; |
| 212 | if (newFirst < 0) { |
| 213 | int index = location + firstIndex; |
| 214 | System.arraycopy(array, index, array, index - newFirst, |
| 215 | size - location); |
| 216 | newFirst = 0; |
| 217 | } |
| 218 | System.arraycopy(array, firstIndex, array, newFirst, location); |
| 219 | firstIndex = newFirst; |
| 220 | } else { |
| 221 | int index = location + firstIndex; |
| 222 | System.arraycopy(array, index, array, index + growSize, size |
| 223 | - location); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | System.arraycopy(dumparray, 0, this.array, location + firstIndex, |
| 228 | growSize); |
| 229 | size += growSize; |
| 230 | modCount++; |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Adds the objects in the specified collection to this {@code ArrayList}. |