268 * specified array, it is returned therein. Otherwise, a new array is
269 * allocated with the runtime type of the specified array and the size of
270 * this list.
271 *
272 * <p>If the list fits in the specified array with room to spare
273 * (i.e., the array has more elements than the list), the element in
274 * the array immediately following the end of the collection is set to
275 * <tt>null</tt>. (This is useful in determining the length of the
276 * list <i>only</i> if the caller knows that the list does not contain
277 * any null elements.)
278 *
279 * @param a the array into which the elements of the list are to
280 * be stored, if it is big enough; otherwise, a new array of the
281 * same runtime type is allocated for this purpose.
282 * @return an array containing the elements of the list
283 * @throws ArrayStoreException if the runtime type of the specified array
284 * is not a supertype of the runtime type of every element in
285 * this list
286 * @throws NullPointerException if the specified array is null
287 */
288 public <T> T[] toArray(T[] a) {
289 if (a.length < size)
290 // Make a new array of a's runtime type, but my contents:
291 return (T[]) Arrays.copyOf(elementData, size, a.getClass());
292 System.arraycopy(elementData, 0, a, 0, size);
293 if (a.length > size)
294 a[size] = null;
295 return a;
296 }
297
298 // Positional Access Operations
299
300 /**
301 * Returns the element at the specified position in this list.
302 *
303 * @param index index of the element to return
304 * @return the element at the specified position in this list
305 * @throws IndexOutOfBoundsException {@inheritDoc}
306 */
307 public E get(int index) {
308 rangeCheck(index);
309
310 return (E) elementData[index];
311 }
312
313 /**
314 * Replaces the element at the specified position in this list with
315 * the specified element.
316 *
317 * @param index index of the element to replace
318 * @param element element to be stored at the specified position
319 * @return the element previously at the specified position
320 * @throws IndexOutOfBoundsException {@inheritDoc}
321 */
322 public E set(int index, E element) {
323 rangeCheck(index);
324
325 E oldValue = (E) elementData[index];
326 elementData[index] = element;
327 return oldValue;
328 }
329
330 /**
331 * Appends the specified element to the end of this list.
332 *
333 * @param e element to be appended to this list
334 * @return <tt>true</tt> (as specified by {@link Collection#add})
335 */
336 public boolean add(E e) {
337 ensureCapacity(size + 1); // Increments modCount!!
338 elementData[size++] = e;
339 return true;
340 }
341
342 /**
343 * Inserts the specified element at the specified position in this
344 * list. Shifts the element currently at that position (if any) and
354 ensureCapacity(size+1); // Increments modCount!!
355 System.arraycopy(elementData, index, elementData, index + 1,
356 size - index);
357 elementData[index] = element;
358 size++;
359 }
360
361 /**
362 * Removes the element at the specified position in this list.
363 * Shifts any subsequent elements to the left (subtracts one from their
364 * indices).
365 *
366 * @param index the index of the element to be removed
367 * @return the element that was removed from the list
368 * @throws IndexOutOfBoundsException {@inheritDoc}
369 */
370 public E remove(int index) {
371 rangeCheck(index);
372
373 modCount++;
374 E oldValue = (E) elementData[index];
375
376 int numMoved = size - index - 1;
377 if (numMoved > 0)
378 System.arraycopy(elementData, index+1, elementData, index,
379 numMoved);
380 elementData[--size] = null; // Let gc do its work
381
382 return oldValue;
383 }
384
385 /**
386 * Removes the first occurrence of the specified element from this list,
387 * if it is present. If the list does not contain the element, it is
388 * unchanged. More formally, removes the element with the lowest index
389 * <tt>i</tt> such that
390 * <tt>(o==null ? get(i)==null : o == get(i))</tt>
391 * (if such an element exists). Returns <tt>true</tt> if this list
392 * contained the specified element (or equivalently, if this list
393 * changed as a result of the call).
|
268 * specified array, it is returned therein. Otherwise, a new array is
269 * allocated with the runtime type of the specified array and the size of
270 * this list.
271 *
272 * <p>If the list fits in the specified array with room to spare
273 * (i.e., the array has more elements than the list), the element in
274 * the array immediately following the end of the collection is set to
275 * <tt>null</tt>. (This is useful in determining the length of the
276 * list <i>only</i> if the caller knows that the list does not contain
277 * any null elements.)
278 *
279 * @param a the array into which the elements of the list are to
280 * be stored, if it is big enough; otherwise, a new array of the
281 * same runtime type is allocated for this purpose.
282 * @return an array containing the elements of the list
283 * @throws ArrayStoreException if the runtime type of the specified array
284 * is not a supertype of the runtime type of every element in
285 * this list
286 * @throws NullPointerException if the specified array is null
287 */
288 @SuppressWarnings("unchecked")
289 public <T> T[] toArray(T[] a) {
290 if (a.length < size)
291 // Make a new array of a's runtime type, but my contents:
292 return (T[]) Arrays.copyOf(elementData, size, a.getClass());
293 System.arraycopy(elementData, 0, a, 0, size);
294 if (a.length > size)
295 a[size] = null;
296 return a;
297 }
298
299 // Positional Access Operations
300
301 /**
302 * Returns the element at the specified position in this list.
303 *
304 * @param index index of the element to return
305 * @return the element at the specified position in this list
306 * @throws IndexOutOfBoundsException {@inheritDoc}
307 */
308 public E get(int index) {
309 rangeCheck(index);
310
311 @SuppressWarnings("unchecked")
312 E rv = (E) elementData[index];
313 return rv;
314 }
315
316 /**
317 * Replaces the element at the specified position in this list with
318 * the specified element.
319 *
320 * @param index index of the element to replace
321 * @param element element to be stored at the specified position
322 * @return the element previously at the specified position
323 * @throws IndexOutOfBoundsException {@inheritDoc}
324 */
325 public E set(int index, E element) {
326 rangeCheck(index);
327
328 @SuppressWarnings("unchecked")
329 E oldValue = (E) elementData[index];
330 elementData[index] = element;
331 return oldValue;
332 }
333
334 /**
335 * Appends the specified element to the end of this list.
336 *
337 * @param e element to be appended to this list
338 * @return <tt>true</tt> (as specified by {@link Collection#add})
339 */
340 public boolean add(E e) {
341 ensureCapacity(size + 1); // Increments modCount!!
342 elementData[size++] = e;
343 return true;
344 }
345
346 /**
347 * Inserts the specified element at the specified position in this
348 * list. Shifts the element currently at that position (if any) and
358 ensureCapacity(size+1); // Increments modCount!!
359 System.arraycopy(elementData, index, elementData, index + 1,
360 size - index);
361 elementData[index] = element;
362 size++;
363 }
364
365 /**
366 * Removes the element at the specified position in this list.
367 * Shifts any subsequent elements to the left (subtracts one from their
368 * indices).
369 *
370 * @param index the index of the element to be removed
371 * @return the element that was removed from the list
372 * @throws IndexOutOfBoundsException {@inheritDoc}
373 */
374 public E remove(int index) {
375 rangeCheck(index);
376
377 modCount++;
378 @SuppressWarnings("unchecked")
379 E oldValue = (E) elementData[index];
380
381 int numMoved = size - index - 1;
382 if (numMoved > 0)
383 System.arraycopy(elementData, index+1, elementData, index,
384 numMoved);
385 elementData[--size] = null; // Let gc do its work
386
387 return oldValue;
388 }
389
390 /**
391 * Removes the first occurrence of the specified element from this list,
392 * if it is present. If the list does not contain the element, it is
393 * unchanged. More formally, removes the element with the lowest index
394 * <tt>i</tt> such that
395 * <tt>(o==null ? get(i)==null : o == get(i))</tt>
396 * (if such an element exists). Returns <tt>true</tt> if this list
397 * contained the specified element (or equivalently, if this list
398 * changed as a result of the call).
|