277 *
278 * @return the number of elements in this list
279 */
280 public int size() {
281 return size;
282 }
283
284 /**
285 * Returns {@code true} if this list contains no elements.
286 *
287 * @return {@code true} if this list contains no elements
288 */
289 public boolean isEmpty() {
290 return size == 0;
291 }
292
293 /**
294 * Returns {@code true} if this list contains the specified element.
295 * More formally, returns {@code true} if and only if this list contains
296 * at least one element {@code e} such that
297 * <tt>(o==null ? e==null : o.equals(e))</tt>.
298 *
299 * @param o element whose presence in this list is to be tested
300 * @return {@code true} if this list contains the specified element
301 */
302 public boolean contains(Object o) {
303 return indexOf(o) >= 0;
304 }
305
306 /**
307 * Returns the index of the first occurrence of the specified element
308 * in this list, or -1 if this list does not contain the element.
309 * More formally, returns the lowest index {@code i} such that
310 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
311 * or -1 if there is no such index.
312 */
313 public int indexOf(Object o) {
314 if (o == null) {
315 for (int i = 0; i < size; i++)
316 if (elementData[i]==null)
317 return i;
318 } else {
319 for (int i = 0; i < size; i++)
320 if (o.equals(elementData[i]))
321 return i;
322 }
323 return -1;
324 }
325
326 /**
327 * Returns the index of the last occurrence of the specified element
328 * in this list, or -1 if this list does not contain the element.
329 * More formally, returns the highest index {@code i} such that
330 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
331 * or -1 if there is no such index.
332 */
333 public int lastIndexOf(Object o) {
334 if (o == null) {
335 for (int i = size-1; i >= 0; i--)
336 if (elementData[i]==null)
337 return i;
338 } else {
339 for (int i = size-1; i >= 0; i--)
340 if (o.equals(elementData[i]))
341 return i;
342 }
343 return -1;
344 }
345
346 /**
347 * Returns a shallow copy of this {@code ArrayList} instance. (The
348 * elements themselves are not copied.)
349 *
350 * @return a clone of this {@code ArrayList} instance
494 public E remove(int index) {
495 rangeCheck(index);
496
497 modCount++;
498 E oldValue = elementData(index);
499
500 int numMoved = size - index - 1;
501 if (numMoved > 0)
502 System.arraycopy(elementData, index+1, elementData, index,
503 numMoved);
504 elementData[--size] = null; // clear to let GC do its work
505
506 return oldValue;
507 }
508
509 /**
510 * Removes the first occurrence of the specified element from this list,
511 * if it is present. If the list does not contain the element, it is
512 * unchanged. More formally, removes the element with the lowest index
513 * {@code i} such that
514 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>
515 * (if such an element exists). Returns {@code true} if this list
516 * contained the specified element (or equivalently, if this list
517 * changed as a result of the call).
518 *
519 * @param o element to be removed from this list, if present
520 * @return {@code true} if this list contained the specified element
521 */
522 public boolean remove(Object o) {
523 if (o == null) {
524 for (int index = 0; index < size; index++)
525 if (elementData[index] == null) {
526 fastRemove(index);
527 return true;
528 }
529 } else {
530 for (int index = 0; index < size; index++)
531 if (o.equals(elementData[index])) {
532 fastRemove(index);
533 return true;
534 }
|
277 *
278 * @return the number of elements in this list
279 */
280 public int size() {
281 return size;
282 }
283
284 /**
285 * Returns {@code true} if this list contains no elements.
286 *
287 * @return {@code true} if this list contains no elements
288 */
289 public boolean isEmpty() {
290 return size == 0;
291 }
292
293 /**
294 * Returns {@code true} if this list contains the specified element.
295 * More formally, returns {@code true} if and only if this list contains
296 * at least one element {@code e} such that
297 * <code>(o==null ? e==null : o.equals(e))</code>.
298 *
299 * @param o element whose presence in this list is to be tested
300 * @return {@code true} if this list contains the specified element
301 */
302 public boolean contains(Object o) {
303 return indexOf(o) >= 0;
304 }
305
306 /**
307 * Returns the index of the first occurrence of the specified element
308 * in this list, or -1 if this list does not contain the element.
309 * More formally, returns the lowest index {@code i} such that
310 * <code>(o==null ? get(i)==null : o.equals(get(i)))</code>,
311 * or -1 if there is no such index.
312 */
313 public int indexOf(Object o) {
314 if (o == null) {
315 for (int i = 0; i < size; i++)
316 if (elementData[i]==null)
317 return i;
318 } else {
319 for (int i = 0; i < size; i++)
320 if (o.equals(elementData[i]))
321 return i;
322 }
323 return -1;
324 }
325
326 /**
327 * Returns the index of the last occurrence of the specified element
328 * in this list, or -1 if this list does not contain the element.
329 * More formally, returns the highest index {@code i} such that
330 * <code>(o==null ? get(i)==null : o.equals(get(i)))</code>,
331 * or -1 if there is no such index.
332 */
333 public int lastIndexOf(Object o) {
334 if (o == null) {
335 for (int i = size-1; i >= 0; i--)
336 if (elementData[i]==null)
337 return i;
338 } else {
339 for (int i = size-1; i >= 0; i--)
340 if (o.equals(elementData[i]))
341 return i;
342 }
343 return -1;
344 }
345
346 /**
347 * Returns a shallow copy of this {@code ArrayList} instance. (The
348 * elements themselves are not copied.)
349 *
350 * @return a clone of this {@code ArrayList} instance
494 public E remove(int index) {
495 rangeCheck(index);
496
497 modCount++;
498 E oldValue = elementData(index);
499
500 int numMoved = size - index - 1;
501 if (numMoved > 0)
502 System.arraycopy(elementData, index+1, elementData, index,
503 numMoved);
504 elementData[--size] = null; // clear to let GC do its work
505
506 return oldValue;
507 }
508
509 /**
510 * Removes the first occurrence of the specified element from this list,
511 * if it is present. If the list does not contain the element, it is
512 * unchanged. More formally, removes the element with the lowest index
513 * {@code i} such that
514 * <code>(o==null ? get(i)==null : o.equals(get(i)))</code>
515 * (if such an element exists). Returns {@code true} if this list
516 * contained the specified element (or equivalently, if this list
517 * changed as a result of the call).
518 *
519 * @param o element to be removed from this list, if present
520 * @return {@code true} if this list contained the specified element
521 */
522 public boolean remove(Object o) {
523 if (o == null) {
524 for (int index = 0; index < size; index++)
525 if (elementData[index] == null) {
526 fastRemove(index);
527 return true;
528 }
529 } else {
530 for (int index = 0; index < size; index++)
531 if (o.equals(elementData[index])) {
532 fastRemove(index);
533 return true;
534 }
|