ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/Vector.java
Revision: 1.32
Committed: Sat Nov 12 20:51:59 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.31: +2 -3 lines
Log Message:
code golf

File Contents

# Content
1 /*
2 * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.util;
27
28 import java.util.function.Consumer;
29 import java.util.function.Predicate;
30 import java.util.function.UnaryOperator;
31
32 /**
33 * The {@code Vector} class implements a growable array of
34 * objects. Like an array, it contains components that can be
35 * accessed using an integer index. However, the size of a
36 * {@code Vector} can grow or shrink as needed to accommodate
37 * adding and removing items after the {@code Vector} has been created.
38 *
39 * <p>Each vector tries to optimize storage management by maintaining a
40 * {@code capacity} and a {@code capacityIncrement}. The
41 * {@code capacity} is always at least as large as the vector
42 * size; it is usually larger because as components are added to the
43 * vector, the vector's storage increases in chunks the size of
44 * {@code capacityIncrement}. An application can increase the
45 * capacity of a vector before inserting a large number of
46 * components; this reduces the amount of incremental reallocation.
47 *
48 * <p id="fail-fast">
49 * The iterators returned by this class's {@link #iterator() iterator} and
50 * {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:
51 * if the vector is structurally modified at any time after the iterator is
52 * created, in any way except through the iterator's own
53 * {@link ListIterator#remove() remove} or
54 * {@link ListIterator#add(Object) add} methods, the iterator will throw a
55 * {@link ConcurrentModificationException}. Thus, in the face of
56 * concurrent modification, the iterator fails quickly and cleanly, rather
57 * than risking arbitrary, non-deterministic behavior at an undetermined
58 * time in the future. The {@link Enumeration Enumerations} returned by
59 * the {@link #elements() elements} method are <em>not</em> fail-fast; if the
60 * Vector is structurally modified at any time after the enumeration is
61 * created then the results of enumerating are undefined.
62 *
63 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
64 * as it is, generally speaking, impossible to make any hard guarantees in the
65 * presence of unsynchronized concurrent modification. Fail-fast iterators
66 * throw {@code ConcurrentModificationException} on a best-effort basis.
67 * Therefore, it would be wrong to write a program that depended on this
68 * exception for its correctness: <i>the fail-fast behavior of iterators
69 * should be used only to detect bugs.</i>
70 *
71 * <p>As of the Java 2 platform v1.2, this class was retrofitted to
72 * implement the {@link List} interface, making it a member of the
73 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
74 * Java Collections Framework</a>. Unlike the new collection
75 * implementations, {@code Vector} is synchronized. If a thread-safe
76 * implementation is not needed, it is recommended to use {@link
77 * ArrayList} in place of {@code Vector}.
78 *
79 * @param <E> Type of component elements
80 *
81 * @author Lee Boynton
82 * @author Jonathan Payne
83 * @see Collection
84 * @see LinkedList
85 * @since 1.0
86 */
87 public class Vector<E>
88 extends AbstractList<E>
89 implements List<E>, RandomAccess, Cloneable, java.io.Serializable
90 {
91 /**
92 * The array buffer into which the components of the vector are
93 * stored. The capacity of the vector is the length of this array buffer,
94 * and is at least large enough to contain all the vector's elements.
95 *
96 * <p>Any array elements following the last element in the Vector are null.
97 *
98 * @serial
99 */
100 protected Object[] elementData;
101
102 /**
103 * The number of valid components in this {@code Vector} object.
104 * Components {@code elementData[0]} through
105 * {@code elementData[elementCount-1]} are the actual items.
106 *
107 * @serial
108 */
109 protected int elementCount;
110
111 /**
112 * The amount by which the capacity of the vector is automatically
113 * incremented when its size becomes greater than its capacity. If
114 * the capacity increment is less than or equal to zero, the capacity
115 * of the vector is doubled each time it needs to grow.
116 *
117 * @serial
118 */
119 protected int capacityIncrement;
120
121 /** use serialVersionUID from JDK 1.0.2 for interoperability */
122 private static final long serialVersionUID = -2767605614048989439L;
123
124 /**
125 * Constructs an empty vector with the specified initial capacity and
126 * capacity increment.
127 *
128 * @param initialCapacity the initial capacity of the vector
129 * @param capacityIncrement the amount by which the capacity is
130 * increased when the vector overflows
131 * @throws IllegalArgumentException if the specified initial capacity
132 * is negative
133 */
134 public Vector(int initialCapacity, int capacityIncrement) {
135 super();
136 if (initialCapacity < 0)
137 throw new IllegalArgumentException("Illegal Capacity: "+
138 initialCapacity);
139 this.elementData = new Object[initialCapacity];
140 this.capacityIncrement = capacityIncrement;
141 }
142
143 /**
144 * Constructs an empty vector with the specified initial capacity and
145 * with its capacity increment equal to zero.
146 *
147 * @param initialCapacity the initial capacity of the vector
148 * @throws IllegalArgumentException if the specified initial capacity
149 * is negative
150 */
151 public Vector(int initialCapacity) {
152 this(initialCapacity, 0);
153 }
154
155 /**
156 * Constructs an empty vector so that its internal data array
157 * has size {@code 10} and its standard capacity increment is
158 * zero.
159 */
160 public Vector() {
161 this(10);
162 }
163
164 /**
165 * Constructs a vector containing the elements of the specified
166 * collection, in the order they are returned by the collection's
167 * iterator.
168 *
169 * @param c the collection whose elements are to be placed into this
170 * vector
171 * @throws NullPointerException if the specified collection is null
172 * @since 1.2
173 */
174 public Vector(Collection<? extends E> c) {
175 elementData = c.toArray();
176 elementCount = elementData.length;
177 // defend against c.toArray (incorrectly) not returning Object[]
178 // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)
179 if (elementData.getClass() != Object[].class)
180 elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
181 }
182
183 /**
184 * Copies the components of this vector into the specified array.
185 * The item at index {@code k} in this vector is copied into
186 * component {@code k} of {@code anArray}.
187 *
188 * @param anArray the array into which the components get copied
189 * @throws NullPointerException if the given array is null
190 * @throws IndexOutOfBoundsException if the specified array is not
191 * large enough to hold all the components of this vector
192 * @throws ArrayStoreException if a component of this vector is not of
193 * a runtime type that can be stored in the specified array
194 * @see #toArray(Object[])
195 */
196 public synchronized void copyInto(Object[] anArray) {
197 System.arraycopy(elementData, 0, anArray, 0, elementCount);
198 }
199
200 /**
201 * Trims the capacity of this vector to be the vector's current
202 * size. If the capacity of this vector is larger than its current
203 * size, then the capacity is changed to equal the size by replacing
204 * its internal data array, kept in the field {@code elementData},
205 * with a smaller one. An application can use this operation to
206 * minimize the storage of a vector.
207 */
208 public synchronized void trimToSize() {
209 modCount++;
210 int oldCapacity = elementData.length;
211 if (elementCount < oldCapacity) {
212 elementData = Arrays.copyOf(elementData, elementCount);
213 }
214 }
215
216 /**
217 * Increases the capacity of this vector, if necessary, to ensure
218 * that it can hold at least the number of components specified by
219 * the minimum capacity argument.
220 *
221 * <p>If the current capacity of this vector is less than
222 * {@code minCapacity}, then its capacity is increased by replacing its
223 * internal data array, kept in the field {@code elementData}, with a
224 * larger one. The size of the new data array will be the old size plus
225 * {@code capacityIncrement}, unless the value of
226 * {@code capacityIncrement} is less than or equal to zero, in which case
227 * the new capacity will be twice the old capacity; but if this new size
228 * is still smaller than {@code minCapacity}, then the new capacity will
229 * be {@code minCapacity}.
230 *
231 * @param minCapacity the desired minimum capacity
232 */
233 public synchronized void ensureCapacity(int minCapacity) {
234 if (minCapacity > 0) {
235 modCount++;
236 if (minCapacity > elementData.length)
237 grow(minCapacity);
238 }
239 }
240
241 /**
242 * The maximum size of array to allocate (unless necessary).
243 * Some VMs reserve some header words in an array.
244 * Attempts to allocate larger arrays may result in
245 * OutOfMemoryError: Requested array size exceeds VM limit
246 */
247 private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
248
249 /**
250 * Increases the capacity to ensure that it can hold at least the
251 * number of elements specified by the minimum capacity argument.
252 *
253 * @param minCapacity the desired minimum capacity
254 * @throws OutOfMemoryError if minCapacity is less than zero
255 */
256 private Object[] grow(int minCapacity) {
257 return elementData = Arrays.copyOf(elementData,
258 newCapacity(minCapacity));
259 }
260
261 private Object[] grow() {
262 return grow(elementCount + 1);
263 }
264
265 /**
266 * Returns a capacity at least as large as the given minimum capacity.
267 * Will not return a capacity greater than MAX_ARRAY_SIZE unless
268 * the given minimum capacity is greater than MAX_ARRAY_SIZE.
269 *
270 * @param minCapacity the desired minimum capacity
271 * @throws OutOfMemoryError if minCapacity is less than zero
272 */
273 private int newCapacity(int minCapacity) {
274 // overflow-conscious code
275 int oldCapacity = elementData.length;
276 int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
277 capacityIncrement : oldCapacity);
278 if (newCapacity - minCapacity <= 0) {
279 if (minCapacity < 0) // overflow
280 throw new OutOfMemoryError();
281 return minCapacity;
282 }
283 return (newCapacity - MAX_ARRAY_SIZE <= 0)
284 ? newCapacity
285 : hugeCapacity(minCapacity);
286 }
287
288 private static int hugeCapacity(int minCapacity) {
289 if (minCapacity < 0) // overflow
290 throw new OutOfMemoryError();
291 return (minCapacity > MAX_ARRAY_SIZE) ?
292 Integer.MAX_VALUE :
293 MAX_ARRAY_SIZE;
294 }
295
296 /**
297 * Sets the size of this vector. If the new size is greater than the
298 * current size, new {@code null} items are added to the end of
299 * the vector. If the new size is less than the current size, all
300 * components at index {@code newSize} and greater are discarded.
301 *
302 * @param newSize the new size of this vector
303 * @throws ArrayIndexOutOfBoundsException if the new size is negative
304 */
305 public synchronized void setSize(int newSize) {
306 modCount++;
307 if (newSize > elementData.length)
308 grow(newSize);
309 for (int i = newSize; i < elementCount; i++)
310 elementData[i] = null;
311 elementCount = newSize;
312 }
313
314 /**
315 * Returns the current capacity of this vector.
316 *
317 * @return the current capacity (the length of its internal
318 * data array, kept in the field {@code elementData}
319 * of this vector)
320 */
321 public synchronized int capacity() {
322 return elementData.length;
323 }
324
325 /**
326 * Returns the number of components in this vector.
327 *
328 * @return the number of components in this vector
329 */
330 public synchronized int size() {
331 return elementCount;
332 }
333
334 /**
335 * Tests if this vector has no components.
336 *
337 * @return {@code true} if and only if this vector has
338 * no components, that is, its size is zero;
339 * {@code false} otherwise.
340 */
341 public synchronized boolean isEmpty() {
342 return elementCount == 0;
343 }
344
345 /**
346 * Returns an enumeration of the components of this vector. The
347 * returned {@code Enumeration} object will generate all items in
348 * this vector. The first item generated is the item at index {@code 0},
349 * then the item at index {@code 1}, and so on. If the vector is
350 * structurally modified while enumerating over the elements then the
351 * results of enumerating are undefined.
352 *
353 * @return an enumeration of the components of this vector
354 * @see Iterator
355 */
356 public Enumeration<E> elements() {
357 return new Enumeration<E>() {
358 int count = 0;
359
360 public boolean hasMoreElements() {
361 return count < elementCount;
362 }
363
364 public E nextElement() {
365 synchronized (Vector.this) {
366 if (count < elementCount) {
367 return elementData(count++);
368 }
369 }
370 throw new NoSuchElementException("Vector Enumeration");
371 }
372 };
373 }
374
375 /**
376 * Returns {@code true} if this vector contains the specified element.
377 * More formally, returns {@code true} if and only if this vector
378 * contains at least one element {@code e} such that
379 * {@code Objects.equals(o, e)}.
380 *
381 * @param o element whose presence in this vector is to be tested
382 * @return {@code true} if this vector contains the specified element
383 */
384 public boolean contains(Object o) {
385 return indexOf(o, 0) >= 0;
386 }
387
388 /**
389 * Returns the index of the first occurrence of the specified element
390 * in this vector, or -1 if this vector does not contain the element.
391 * More formally, returns the lowest index {@code i} such that
392 * {@code Objects.equals(o, get(i))},
393 * or -1 if there is no such index.
394 *
395 * @param o element to search for
396 * @return the index of the first occurrence of the specified element in
397 * this vector, or -1 if this vector does not contain the element
398 */
399 public int indexOf(Object o) {
400 return indexOf(o, 0);
401 }
402
403 /**
404 * Returns the index of the first occurrence of the specified element in
405 * this vector, searching forwards from {@code index}, or returns -1 if
406 * the element is not found.
407 * More formally, returns the lowest index {@code i} such that
408 * {@code (i >= index && Objects.equals(o, get(i)))},
409 * or -1 if there is no such index.
410 *
411 * @param o element to search for
412 * @param index index to start searching from
413 * @return the index of the first occurrence of the element in
414 * this vector at position {@code index} or later in the vector;
415 * {@code -1} if the element is not found.
416 * @throws IndexOutOfBoundsException if the specified index is negative
417 * @see Object#equals(Object)
418 */
419 public synchronized int indexOf(Object o, int index) {
420 if (o == null) {
421 for (int i = index ; i < elementCount ; i++)
422 if (elementData[i]==null)
423 return i;
424 } else {
425 for (int i = index ; i < elementCount ; i++)
426 if (o.equals(elementData[i]))
427 return i;
428 }
429 return -1;
430 }
431
432 /**
433 * Returns the index of the last occurrence of the specified element
434 * in this vector, or -1 if this vector does not contain the element.
435 * More formally, returns the highest index {@code i} such that
436 * {@code Objects.equals(o, get(i))},
437 * or -1 if there is no such index.
438 *
439 * @param o element to search for
440 * @return the index of the last occurrence of the specified element in
441 * this vector, or -1 if this vector does not contain the element
442 */
443 public synchronized int lastIndexOf(Object o) {
444 return lastIndexOf(o, elementCount-1);
445 }
446
447 /**
448 * Returns the index of the last occurrence of the specified element in
449 * this vector, searching backwards from {@code index}, or returns -1 if
450 * the element is not found.
451 * More formally, returns the highest index {@code i} such that
452 * {@code (i <= index && Objects.equals(o, get(i)))},
453 * or -1 if there is no such index.
454 *
455 * @param o element to search for
456 * @param index index to start searching backwards from
457 * @return the index of the last occurrence of the element at position
458 * less than or equal to {@code index} in this vector;
459 * -1 if the element is not found.
460 * @throws IndexOutOfBoundsException if the specified index is greater
461 * than or equal to the current size of this vector
462 */
463 public synchronized int lastIndexOf(Object o, int index) {
464 if (index >= elementCount)
465 throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
466
467 if (o == null) {
468 for (int i = index; i >= 0; i--)
469 if (elementData[i]==null)
470 return i;
471 } else {
472 for (int i = index; i >= 0; i--)
473 if (o.equals(elementData[i]))
474 return i;
475 }
476 return -1;
477 }
478
479 /**
480 * Returns the component at the specified index.
481 *
482 * <p>This method is identical in functionality to the {@link #get(int)}
483 * method (which is part of the {@link List} interface).
484 *
485 * @param index an index into this vector
486 * @return the component at the specified index
487 * @throws ArrayIndexOutOfBoundsException if the index is out of range
488 * ({@code index < 0 || index >= size()})
489 */
490 public synchronized E elementAt(int index) {
491 if (index >= elementCount) {
492 throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
493 }
494
495 return elementData(index);
496 }
497
498 /**
499 * Returns the first component (the item at index {@code 0}) of
500 * this vector.
501 *
502 * @return the first component of this vector
503 * @throws NoSuchElementException if this vector has no components
504 */
505 public synchronized E firstElement() {
506 if (elementCount == 0) {
507 throw new NoSuchElementException();
508 }
509 return elementData(0);
510 }
511
512 /**
513 * Returns the last component of the vector.
514 *
515 * @return the last component of the vector, i.e., the component at index
516 * {@code size() - 1}
517 * @throws NoSuchElementException if this vector is empty
518 */
519 public synchronized E lastElement() {
520 if (elementCount == 0) {
521 throw new NoSuchElementException();
522 }
523 return elementData(elementCount - 1);
524 }
525
526 /**
527 * Sets the component at the specified {@code index} of this
528 * vector to be the specified object. The previous component at that
529 * position is discarded.
530 *
531 * <p>The index must be a value greater than or equal to {@code 0}
532 * and less than the current size of the vector.
533 *
534 * <p>This method is identical in functionality to the
535 * {@link #set(int, Object) set(int, E)}
536 * method (which is part of the {@link List} interface). Note that the
537 * {@code set} method reverses the order of the parameters, to more closely
538 * match array usage. Note also that the {@code set} method returns the
539 * old value that was stored at the specified position.
540 *
541 * @param obj what the component is to be set to
542 * @param index the specified index
543 * @throws ArrayIndexOutOfBoundsException if the index is out of range
544 * ({@code index < 0 || index >= size()})
545 */
546 public synchronized void setElementAt(E obj, int index) {
547 if (index >= elementCount) {
548 throw new ArrayIndexOutOfBoundsException(index + " >= " +
549 elementCount);
550 }
551 elementData[index] = obj;
552 }
553
554 /**
555 * Deletes the component at the specified index. Each component in
556 * this vector with an index greater or equal to the specified
557 * {@code index} is shifted downward to have an index one
558 * smaller than the value it had previously. The size of this vector
559 * is decreased by {@code 1}.
560 *
561 * <p>The index must be a value greater than or equal to {@code 0}
562 * and less than the current size of the vector.
563 *
564 * <p>This method is identical in functionality to the {@link #remove(int)}
565 * method (which is part of the {@link List} interface). Note that the
566 * {@code remove} method returns the old value that was stored at the
567 * specified position.
568 *
569 * @param index the index of the object to remove
570 * @throws ArrayIndexOutOfBoundsException if the index is out of range
571 * ({@code index < 0 || index >= size()})
572 */
573 public synchronized void removeElementAt(int index) {
574 if (index >= elementCount) {
575 throw new ArrayIndexOutOfBoundsException(index + " >= " +
576 elementCount);
577 }
578 else if (index < 0) {
579 throw new ArrayIndexOutOfBoundsException(index);
580 }
581 int j = elementCount - index - 1;
582 if (j > 0) {
583 System.arraycopy(elementData, index + 1, elementData, index, j);
584 }
585 modCount++;
586 elementCount--;
587 elementData[elementCount] = null; /* to let gc do its work */
588 }
589
590 /**
591 * Inserts the specified object as a component in this vector at the
592 * specified {@code index}. Each component in this vector with
593 * an index greater or equal to the specified {@code index} is
594 * shifted upward to have an index one greater than the value it had
595 * previously.
596 *
597 * <p>The index must be a value greater than or equal to {@code 0}
598 * and less than or equal to the current size of the vector. (If the
599 * index is equal to the current size of the vector, the new element
600 * is appended to the Vector.)
601 *
602 * <p>This method is identical in functionality to the
603 * {@link #add(int, Object) add(int, E)}
604 * method (which is part of the {@link List} interface). Note that the
605 * {@code add} method reverses the order of the parameters, to more closely
606 * match array usage.
607 *
608 * @param obj the component to insert
609 * @param index where to insert the new component
610 * @throws ArrayIndexOutOfBoundsException if the index is out of range
611 * ({@code index < 0 || index > size()})
612 */
613 public synchronized void insertElementAt(E obj, int index) {
614 if (index > elementCount) {
615 throw new ArrayIndexOutOfBoundsException(index
616 + " > " + elementCount);
617 }
618 modCount++;
619 final int s = elementCount;
620 Object[] elementData = this.elementData;
621 if (s == elementData.length)
622 elementData = grow();
623 System.arraycopy(elementData, index,
624 elementData, index + 1,
625 s - index);
626 elementData[index] = obj;
627 elementCount = s + 1;
628 }
629
630 /**
631 * Adds the specified component to the end of this vector,
632 * increasing its size by one. The capacity of this vector is
633 * increased if its size becomes greater than its capacity.
634 *
635 * <p>This method is identical in functionality to the
636 * {@link #add(Object) add(E)}
637 * method (which is part of the {@link List} interface).
638 *
639 * @param obj the component to be added
640 */
641 public synchronized void addElement(E obj) {
642 modCount++;
643 add(obj, elementData, elementCount);
644 }
645
646 /**
647 * Removes the first (lowest-indexed) occurrence of the argument
648 * from this vector. If the object is found in this vector, each
649 * component in the vector with an index greater or equal to the
650 * object's index is shifted downward to have an index one smaller
651 * than the value it had previously.
652 *
653 * <p>This method is identical in functionality to the
654 * {@link #remove(Object)} method (which is part of the
655 * {@link List} interface).
656 *
657 * @param obj the component to be removed
658 * @return {@code true} if the argument was a component of this
659 * vector; {@code false} otherwise.
660 */
661 public synchronized boolean removeElement(Object obj) {
662 modCount++;
663 int i = indexOf(obj);
664 if (i >= 0) {
665 removeElementAt(i);
666 return true;
667 }
668 return false;
669 }
670
671 /**
672 * Removes all components from this vector and sets its size to zero.
673 *
674 * <p>This method is identical in functionality to the {@link #clear}
675 * method (which is part of the {@link List} interface).
676 */
677 public synchronized void removeAllElements() {
678 // Let gc do its work
679 for (int i = 0; i < elementCount; i++)
680 elementData[i] = null;
681
682 modCount++;
683 elementCount = 0;
684 }
685
686 /**
687 * Returns a clone of this vector. The copy will contain a
688 * reference to a clone of the internal data array, not a reference
689 * to the original internal data array of this {@code Vector} object.
690 *
691 * @return a clone of this vector
692 */
693 public synchronized Object clone() {
694 try {
695 @SuppressWarnings("unchecked")
696 Vector<E> v = (Vector<E>) super.clone();
697 v.elementData = Arrays.copyOf(elementData, elementCount);
698 v.modCount = 0;
699 return v;
700 } catch (CloneNotSupportedException e) {
701 // this shouldn't happen, since we are Cloneable
702 throw new InternalError(e);
703 }
704 }
705
706 /**
707 * Returns an array containing all of the elements in this Vector
708 * in the correct order.
709 *
710 * @since 1.2
711 */
712 public synchronized Object[] toArray() {
713 return Arrays.copyOf(elementData, elementCount);
714 }
715
716 /**
717 * Returns an array containing all of the elements in this Vector in the
718 * correct order; the runtime type of the returned array is that of the
719 * specified array. If the Vector fits in the specified array, it is
720 * returned therein. Otherwise, a new array is allocated with the runtime
721 * type of the specified array and the size of this Vector.
722 *
723 * <p>If the Vector fits in the specified array with room to spare
724 * (i.e., the array has more elements than the Vector),
725 * the element in the array immediately following the end of the
726 * Vector is set to null. (This is useful in determining the length
727 * of the Vector <em>only</em> if the caller knows that the Vector
728 * does not contain any null elements.)
729 *
730 * @param <T> type of array elements. The same type as {@code <E>} or a
731 * supertype of {@code <E>}.
732 * @param a the array into which the elements of the Vector are to
733 * be stored, if it is big enough; otherwise, a new array of the
734 * same runtime type is allocated for this purpose.
735 * @return an array containing the elements of the Vector
736 * @throws ArrayStoreException if the runtime type of a, {@code <T>}, is not
737 * a supertype of the runtime type, {@code <E>}, of every element in this
738 * Vector
739 * @throws NullPointerException if the given array is null
740 * @since 1.2
741 */
742 @SuppressWarnings("unchecked")
743 public synchronized <T> T[] toArray(T[] a) {
744 if (a.length < elementCount)
745 return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
746
747 System.arraycopy(elementData, 0, a, 0, elementCount);
748
749 if (a.length > elementCount)
750 a[elementCount] = null;
751
752 return a;
753 }
754
755 // Positional Access Operations
756
757 @SuppressWarnings("unchecked")
758 E elementData(int index) {
759 return (E) elementData[index];
760 }
761
762 /**
763 * Returns the element at the specified position in this Vector.
764 *
765 * @param index index of the element to return
766 * @return object at the specified index
767 * @throws ArrayIndexOutOfBoundsException if the index is out of range
768 * ({@code index < 0 || index >= size()})
769 * @since 1.2
770 */
771 public synchronized E get(int index) {
772 if (index >= elementCount)
773 throw new ArrayIndexOutOfBoundsException(index);
774
775 return elementData(index);
776 }
777
778 /**
779 * Replaces the element at the specified position in this Vector with the
780 * specified element.
781 *
782 * @param index index of the element to replace
783 * @param element element to be stored at the specified position
784 * @return the element previously at the specified position
785 * @throws ArrayIndexOutOfBoundsException if the index is out of range
786 * ({@code index < 0 || index >= size()})
787 * @since 1.2
788 */
789 public synchronized E set(int index, E element) {
790 if (index >= elementCount)
791 throw new ArrayIndexOutOfBoundsException(index);
792
793 E oldValue = elementData(index);
794 elementData[index] = element;
795 return oldValue;
796 }
797
798 /**
799 * This helper method split out from add(E) to keep method
800 * bytecode size under 35 (the -XX:MaxInlineSize default value),
801 * which helps when add(E) is called in a C1-compiled loop.
802 */
803 private void add(E e, Object[] elementData, int s) {
804 if (s == elementData.length)
805 elementData = grow();
806 elementData[s] = e;
807 elementCount = s + 1;
808 }
809
810 /**
811 * Appends the specified element to the end of this Vector.
812 *
813 * @param e element to be appended to this Vector
814 * @return {@code true} (as specified by {@link Collection#add})
815 * @since 1.2
816 */
817 public synchronized boolean add(E e) {
818 modCount++;
819 add(e, elementData, elementCount);
820 return true;
821 }
822
823 /**
824 * Removes the first occurrence of the specified element in this Vector
825 * If the Vector does not contain the element, it is unchanged. More
826 * formally, removes the element with the lowest index i such that
827 * {@code Objects.equals(o, get(i))} (if such
828 * an element exists).
829 *
830 * @param o element to be removed from this Vector, if present
831 * @return true if the Vector contained the specified element
832 * @since 1.2
833 */
834 public boolean remove(Object o) {
835 return removeElement(o);
836 }
837
838 /**
839 * Inserts the specified element at the specified position in this Vector.
840 * Shifts the element currently at that position (if any) and any
841 * subsequent elements to the right (adds one to their indices).
842 *
843 * @param index index at which the specified element is to be inserted
844 * @param element element to be inserted
845 * @throws ArrayIndexOutOfBoundsException if the index is out of range
846 * ({@code index < 0 || index > size()})
847 * @since 1.2
848 */
849 public void add(int index, E element) {
850 insertElementAt(element, index);
851 }
852
853 /**
854 * Removes the element at the specified position in this Vector.
855 * Shifts any subsequent elements to the left (subtracts one from their
856 * indices). Returns the element that was removed from the Vector.
857 *
858 * @param index the index of the element to be removed
859 * @return element that was removed
860 * @throws ArrayIndexOutOfBoundsException if the index is out of range
861 * ({@code index < 0 || index >= size()})
862 * @since 1.2
863 */
864 public synchronized E remove(int index) {
865 modCount++;
866 if (index >= elementCount)
867 throw new ArrayIndexOutOfBoundsException(index);
868 E oldValue = elementData(index);
869
870 int numMoved = elementCount - index - 1;
871 if (numMoved > 0)
872 System.arraycopy(elementData, index+1, elementData, index,
873 numMoved);
874 elementData[--elementCount] = null; // Let gc do its work
875
876 return oldValue;
877 }
878
879 /**
880 * Removes all of the elements from this Vector. The Vector will
881 * be empty after this call returns (unless it throws an exception).
882 *
883 * @since 1.2
884 */
885 public void clear() {
886 removeAllElements();
887 }
888
889 // Bulk Operations
890
891 /**
892 * Returns true if this Vector contains all of the elements in the
893 * specified Collection.
894 *
895 * @param c a collection whose elements will be tested for containment
896 * in this Vector
897 * @return true if this Vector contains all of the elements in the
898 * specified collection
899 * @throws NullPointerException if the specified collection is null
900 */
901 public synchronized boolean containsAll(Collection<?> c) {
902 return super.containsAll(c);
903 }
904
905 /**
906 * Appends all of the elements in the specified Collection to the end of
907 * this Vector, in the order that they are returned by the specified
908 * Collection's Iterator. The behavior of this operation is undefined if
909 * the specified Collection is modified while the operation is in progress.
910 * (This implies that the behavior of this call is undefined if the
911 * specified Collection is this Vector, and this Vector is nonempty.)
912 *
913 * @param c elements to be inserted into this Vector
914 * @return {@code true} if this Vector changed as a result of the call
915 * @throws NullPointerException if the specified collection is null
916 * @since 1.2
917 */
918 public boolean addAll(Collection<? extends E> c) {
919 Object[] a = c.toArray();
920 modCount++;
921 int numNew = a.length;
922 if (numNew == 0)
923 return false;
924 synchronized (this) {
925 Object[] elementData = this.elementData;
926 final int s = elementCount;
927 if (numNew > elementData.length - s)
928 elementData = grow(s + numNew);
929 System.arraycopy(a, 0, elementData, s, numNew);
930 elementCount = s + numNew;
931 return true;
932 }
933 }
934
935 /**
936 * Removes from this Vector all of its elements that are contained in the
937 * specified Collection.
938 *
939 * @param c a collection of elements to be removed from the Vector
940 * @return true if this Vector changed as a result of the call
941 * @throws ClassCastException if the types of one or more elements
942 * in this vector are incompatible with the specified
943 * collection
944 * (<a href="Collection.html#optional-restrictions">optional</a>)
945 * @throws NullPointerException if this vector contains one or more null
946 * elements and the specified collection does not support null
947 * elements
948 * (<a href="Collection.html#optional-restrictions">optional</a>),
949 * or if the specified collection is null
950 * @since 1.2
951 */
952 public boolean removeAll(Collection<?> c) {
953 Objects.requireNonNull(c);
954 return bulkRemove(e -> c.contains(e));
955 }
956
957 /**
958 * Retains only the elements in this Vector that are contained in the
959 * specified Collection. In other words, removes from this Vector all
960 * of its elements that are not contained in the specified Collection.
961 *
962 * @param c a collection of elements to be retained in this Vector
963 * (all other elements are removed)
964 * @return true if this Vector changed as a result of the call
965 * @throws ClassCastException if the types of one or more elements
966 * in this vector are incompatible with the specified
967 * collection
968 * (<a href="Collection.html#optional-restrictions">optional</a>)
969 * @throws NullPointerException if this vector contains one or more null
970 * elements and the specified collection does not support null
971 * elements
972 * (<a href="Collection.html#optional-restrictions">optional</a>),
973 * or if the specified collection is null
974 * @since 1.2
975 */
976 public boolean retainAll(Collection<?> c) {
977 Objects.requireNonNull(c);
978 return bulkRemove(e -> !c.contains(e));
979 }
980
981 @Override
982 public boolean removeIf(Predicate<? super E> filter) {
983 Objects.requireNonNull(filter);
984 return bulkRemove(filter);
985 }
986
987 @SuppressWarnings("unchecked")
988 private synchronized boolean bulkRemove(Predicate<? super E> filter) {
989 int expectedModCount = modCount;
990 final Object[] es = elementData;
991 final int size = elementCount;
992 final boolean modified;
993 int r;
994 // Optimize for initial run of survivors
995 for (r = 0; r < size && !filter.test((E) es[r]); r++)
996 ;
997 if (modified = (r < size)) {
998 expectedModCount++;
999 modCount++;
1000 int w = r++;
1001 try {
1002 for (E e; r < size; r++)
1003 if (!filter.test(e = (E) es[r]))
1004 es[w++] = e;
1005 } catch (Throwable ex) {
1006 // copy remaining elements
1007 System.arraycopy(es, r, es, w, size - r);
1008 w += size - r;
1009 throw ex;
1010 } finally {
1011 Arrays.fill(es, elementCount = w, size, null);
1012 }
1013 }
1014 if (modCount != expectedModCount)
1015 throw new ConcurrentModificationException();
1016 return modified;
1017 }
1018
1019 /**
1020 * Inserts all of the elements in the specified Collection into this
1021 * Vector at the specified position. Shifts the element currently at
1022 * that position (if any) and any subsequent elements to the right
1023 * (increases their indices). The new elements will appear in the Vector
1024 * in the order that they are returned by the specified Collection's
1025 * iterator.
1026 *
1027 * @param index index at which to insert the first element from the
1028 * specified collection
1029 * @param c elements to be inserted into this Vector
1030 * @return {@code true} if this Vector changed as a result of the call
1031 * @throws ArrayIndexOutOfBoundsException if the index is out of range
1032 * ({@code index < 0 || index > size()})
1033 * @throws NullPointerException if the specified collection is null
1034 * @since 1.2
1035 */
1036 public synchronized boolean addAll(int index, Collection<? extends E> c) {
1037 if (index < 0 || index > elementCount)
1038 throw new ArrayIndexOutOfBoundsException(index);
1039
1040 Object[] a = c.toArray();
1041 modCount++;
1042 int numNew = a.length;
1043 if (numNew == 0)
1044 return false;
1045 Object[] elementData = this.elementData;
1046 final int s = elementCount;
1047 if (numNew > elementData.length - s)
1048 elementData = grow(s + numNew);
1049
1050 int numMoved = s - index;
1051 if (numMoved > 0)
1052 System.arraycopy(elementData, index,
1053 elementData, index + numNew,
1054 numMoved);
1055 System.arraycopy(a, 0, elementData, index, numNew);
1056 elementCount = s + numNew;
1057 return true;
1058 }
1059
1060 /**
1061 * Compares the specified Object with this Vector for equality. Returns
1062 * true if and only if the specified Object is also a List, both Lists
1063 * have the same size, and all corresponding pairs of elements in the two
1064 * Lists are <em>equal</em>. (Two elements {@code e1} and
1065 * {@code e2} are <em>equal</em> if {@code Objects.equals(e1, e2)}.)
1066 * In other words, two Lists are defined to be
1067 * equal if they contain the same elements in the same order.
1068 *
1069 * @param o the Object to be compared for equality with this Vector
1070 * @return true if the specified Object is equal to this Vector
1071 */
1072 public synchronized boolean equals(Object o) {
1073 return super.equals(o);
1074 }
1075
1076 /**
1077 * Returns the hash code value for this Vector.
1078 */
1079 public synchronized int hashCode() {
1080 return super.hashCode();
1081 }
1082
1083 /**
1084 * Returns a string representation of this Vector, containing
1085 * the String representation of each element.
1086 */
1087 public synchronized String toString() {
1088 return super.toString();
1089 }
1090
1091 /**
1092 * Returns a view of the portion of this List between fromIndex,
1093 * inclusive, and toIndex, exclusive. (If fromIndex and toIndex are
1094 * equal, the returned List is empty.) The returned List is backed by this
1095 * List, so changes in the returned List are reflected in this List, and
1096 * vice-versa. The returned List supports all of the optional List
1097 * operations supported by this List.
1098 *
1099 * <p>This method eliminates the need for explicit range operations (of
1100 * the sort that commonly exist for arrays). Any operation that expects
1101 * a List can be used as a range operation by operating on a subList view
1102 * instead of a whole List. For example, the following idiom
1103 * removes a range of elements from a List:
1104 * <pre>
1105 * list.subList(from, to).clear();
1106 * </pre>
1107 * Similar idioms may be constructed for indexOf and lastIndexOf,
1108 * and all of the algorithms in the Collections class can be applied to
1109 * a subList.
1110 *
1111 * <p>The semantics of the List returned by this method become undefined if
1112 * the backing list (i.e., this List) is <i>structurally modified</i> in
1113 * any way other than via the returned List. (Structural modifications are
1114 * those that change the size of the List, or otherwise perturb it in such
1115 * a fashion that iterations in progress may yield incorrect results.)
1116 *
1117 * @param fromIndex low endpoint (inclusive) of the subList
1118 * @param toIndex high endpoint (exclusive) of the subList
1119 * @return a view of the specified range within this List
1120 * @throws IndexOutOfBoundsException if an endpoint index value is out of range
1121 * {@code (fromIndex < 0 || toIndex > size)}
1122 * @throws IllegalArgumentException if the endpoint indices are out of order
1123 * {@code (fromIndex > toIndex)}
1124 */
1125 public synchronized List<E> subList(int fromIndex, int toIndex) {
1126 return Collections.synchronizedList(super.subList(fromIndex, toIndex),
1127 this);
1128 }
1129
1130 /**
1131 * Removes from this list all of the elements whose index is between
1132 * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
1133 * Shifts any succeeding elements to the left (reduces their index).
1134 * This call shortens the list by {@code (toIndex - fromIndex)} elements.
1135 * (If {@code toIndex==fromIndex}, this operation has no effect.)
1136 */
1137 protected synchronized void removeRange(int fromIndex, int toIndex) {
1138 int numMoved = elementCount - toIndex;
1139 System.arraycopy(elementData, toIndex, elementData, fromIndex,
1140 numMoved);
1141
1142 // Let gc do its work
1143 modCount++;
1144 int newElementCount = elementCount - (toIndex-fromIndex);
1145 while (elementCount != newElementCount)
1146 elementData[--elementCount] = null;
1147 }
1148
1149 /**
1150 * Save the state of the {@code Vector} instance to a stream (that
1151 * is, serialize it).
1152 * This method performs synchronization to ensure the consistency
1153 * of the serialized data.
1154 */
1155 private void writeObject(java.io.ObjectOutputStream s)
1156 throws java.io.IOException {
1157 final java.io.ObjectOutputStream.PutField fields = s.putFields();
1158 final Object[] data;
1159 synchronized (this) {
1160 fields.put("capacityIncrement", capacityIncrement);
1161 fields.put("elementCount", elementCount);
1162 data = elementData.clone();
1163 }
1164 fields.put("elementData", data);
1165 s.writeFields();
1166 }
1167
1168 /**
1169 * Returns a list iterator over the elements in this list (in proper
1170 * sequence), starting at the specified position in the list.
1171 * The specified index indicates the first element that would be
1172 * returned by an initial call to {@link ListIterator#next next}.
1173 * An initial call to {@link ListIterator#previous previous} would
1174 * return the element with the specified index minus one.
1175 *
1176 * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
1177 *
1178 * @throws IndexOutOfBoundsException {@inheritDoc}
1179 */
1180 public synchronized ListIterator<E> listIterator(int index) {
1181 if (index < 0 || index > elementCount)
1182 throw new IndexOutOfBoundsException("Index: "+index);
1183 return new ListItr(index);
1184 }
1185
1186 /**
1187 * Returns a list iterator over the elements in this list (in proper
1188 * sequence).
1189 *
1190 * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
1191 *
1192 * @see #listIterator(int)
1193 */
1194 public synchronized ListIterator<E> listIterator() {
1195 return new ListItr(0);
1196 }
1197
1198 /**
1199 * Returns an iterator over the elements in this list in proper sequence.
1200 *
1201 * <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
1202 *
1203 * @return an iterator over the elements in this list in proper sequence
1204 */
1205 public synchronized Iterator<E> iterator() {
1206 return new Itr();
1207 }
1208
1209 /**
1210 * An optimized version of AbstractList.Itr
1211 */
1212 private class Itr implements Iterator<E> {
1213 int cursor; // index of next element to return
1214 int lastRet = -1; // index of last element returned; -1 if no such
1215 int expectedModCount = modCount;
1216
1217 public boolean hasNext() {
1218 // Racy but within spec, since modifications are checked
1219 // within or after synchronization in next/previous
1220 return cursor != elementCount;
1221 }
1222
1223 public E next() {
1224 synchronized (Vector.this) {
1225 checkForComodification();
1226 int i = cursor;
1227 if (i >= elementCount)
1228 throw new NoSuchElementException();
1229 cursor = i + 1;
1230 return elementData(lastRet = i);
1231 }
1232 }
1233
1234 public void remove() {
1235 if (lastRet == -1)
1236 throw new IllegalStateException();
1237 synchronized (Vector.this) {
1238 checkForComodification();
1239 Vector.this.remove(lastRet);
1240 expectedModCount = modCount;
1241 }
1242 cursor = lastRet;
1243 lastRet = -1;
1244 }
1245
1246 @Override
1247 public void forEachRemaining(Consumer<? super E> action) {
1248 Objects.requireNonNull(action);
1249 synchronized (Vector.this) {
1250 final int size = elementCount;
1251 int i = cursor;
1252 if (i >= size) {
1253 return;
1254 }
1255 @SuppressWarnings("unchecked")
1256 final E[] elementData = (E[]) Vector.this.elementData;
1257 if (i >= elementData.length) {
1258 throw new ConcurrentModificationException();
1259 }
1260 while (i != size && modCount == expectedModCount) {
1261 action.accept(elementData[i++]);
1262 }
1263 // update once at end of iteration to reduce heap write traffic
1264 cursor = i;
1265 lastRet = i - 1;
1266 checkForComodification();
1267 }
1268 }
1269
1270 final void checkForComodification() {
1271 if (modCount != expectedModCount)
1272 throw new ConcurrentModificationException();
1273 }
1274 }
1275
1276 /**
1277 * An optimized version of AbstractList.ListItr
1278 */
1279 final class ListItr extends Itr implements ListIterator<E> {
1280 ListItr(int index) {
1281 super();
1282 cursor = index;
1283 }
1284
1285 public boolean hasPrevious() {
1286 return cursor != 0;
1287 }
1288
1289 public int nextIndex() {
1290 return cursor;
1291 }
1292
1293 public int previousIndex() {
1294 return cursor - 1;
1295 }
1296
1297 public E previous() {
1298 synchronized (Vector.this) {
1299 checkForComodification();
1300 int i = cursor - 1;
1301 if (i < 0)
1302 throw new NoSuchElementException();
1303 cursor = i;
1304 return elementData(lastRet = i);
1305 }
1306 }
1307
1308 public void set(E e) {
1309 if (lastRet == -1)
1310 throw new IllegalStateException();
1311 synchronized (Vector.this) {
1312 checkForComodification();
1313 Vector.this.set(lastRet, e);
1314 }
1315 }
1316
1317 public void add(E e) {
1318 int i = cursor;
1319 synchronized (Vector.this) {
1320 checkForComodification();
1321 Vector.this.add(i, e);
1322 expectedModCount = modCount;
1323 }
1324 cursor = i + 1;
1325 lastRet = -1;
1326 }
1327 }
1328
1329 @Override
1330 public synchronized void forEach(Consumer<? super E> action) {
1331 Objects.requireNonNull(action);
1332 final int expectedModCount = modCount;
1333 @SuppressWarnings("unchecked")
1334 final E[] elementData = (E[]) this.elementData;
1335 final int elementCount = this.elementCount;
1336 for (int i=0; modCount == expectedModCount && i < elementCount; i++) {
1337 action.accept(elementData[i]);
1338 }
1339 if (modCount != expectedModCount) {
1340 throw new ConcurrentModificationException();
1341 }
1342 }
1343
1344 @Override
1345 @SuppressWarnings("unchecked")
1346 public synchronized void replaceAll(UnaryOperator<E> operator) {
1347 Objects.requireNonNull(operator);
1348 final int expectedModCount = modCount;
1349 final int size = elementCount;
1350 for (int i=0; modCount == expectedModCount && i < size; i++) {
1351 elementData[i] = operator.apply((E) elementData[i]);
1352 }
1353 if (modCount != expectedModCount) {
1354 throw new ConcurrentModificationException();
1355 }
1356 modCount++;
1357 }
1358
1359 @SuppressWarnings("unchecked")
1360 @Override
1361 public synchronized void sort(Comparator<? super E> c) {
1362 final int expectedModCount = modCount;
1363 Arrays.sort((E[]) elementData, 0, elementCount, c);
1364 if (modCount != expectedModCount) {
1365 throw new ConcurrentModificationException();
1366 }
1367 modCount++;
1368 }
1369
1370 /**
1371 * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
1372 * and <em>fail-fast</em> {@link Spliterator} over the elements in this
1373 * list.
1374 *
1375 * <p>The {@code Spliterator} reports {@link Spliterator#SIZED},
1376 * {@link Spliterator#SUBSIZED}, and {@link Spliterator#ORDERED}.
1377 * Overriding implementations should document the reporting of additional
1378 * characteristic values.
1379 *
1380 * @return a {@code Spliterator} over the elements in this list
1381 * @since 1.8
1382 */
1383 @Override
1384 public Spliterator<E> spliterator() {
1385 return new VectorSpliterator<>(this, null, 0, -1, 0);
1386 }
1387
1388 /** Similar to ArrayList Spliterator */
1389 static final class VectorSpliterator<E> implements Spliterator<E> {
1390 private final Vector<E> list;
1391 private Object[] array;
1392 private int index; // current index, modified on advance/split
1393 private int fence; // -1 until used; then one past last index
1394 private int expectedModCount; // initialized when fence set
1395
1396 /** Create new spliterator covering the given range */
1397 VectorSpliterator(Vector<E> list, Object[] array, int origin, int fence,
1398 int expectedModCount) {
1399 this.list = list;
1400 this.array = array;
1401 this.index = origin;
1402 this.fence = fence;
1403 this.expectedModCount = expectedModCount;
1404 }
1405
1406 private int getFence() { // initialize on first use
1407 int hi;
1408 if ((hi = fence) < 0) {
1409 synchronized (list) {
1410 array = list.elementData;
1411 expectedModCount = list.modCount;
1412 hi = fence = list.elementCount;
1413 }
1414 }
1415 return hi;
1416 }
1417
1418 public Spliterator<E> trySplit() {
1419 int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
1420 return (lo >= mid) ? null :
1421 new VectorSpliterator<>(list, array, lo, index = mid,
1422 expectedModCount);
1423 }
1424
1425 @SuppressWarnings("unchecked")
1426 public boolean tryAdvance(Consumer<? super E> action) {
1427 int i;
1428 if (action == null)
1429 throw new NullPointerException();
1430 if (getFence() > (i = index)) {
1431 index = i + 1;
1432 action.accept((E)array[i]);
1433 if (list.modCount != expectedModCount)
1434 throw new ConcurrentModificationException();
1435 return true;
1436 }
1437 return false;
1438 }
1439
1440 @SuppressWarnings("unchecked")
1441 public void forEachRemaining(Consumer<? super E> action) {
1442 int i, hi; // hoist accesses and checks from loop
1443 Vector<E> lst; Object[] a;
1444 if (action == null)
1445 throw new NullPointerException();
1446 if ((lst = list) != null) {
1447 if ((hi = fence) < 0) {
1448 synchronized (lst) {
1449 expectedModCount = lst.modCount;
1450 a = array = lst.elementData;
1451 hi = fence = lst.elementCount;
1452 }
1453 }
1454 else
1455 a = array;
1456 if (a != null && (i = index) >= 0 && (index = hi) <= a.length) {
1457 while (i < hi)
1458 action.accept((E) a[i++]);
1459 if (lst.modCount == expectedModCount)
1460 return;
1461 }
1462 }
1463 throw new ConcurrentModificationException();
1464 }
1465
1466 public long estimateSize() {
1467 return getFence() - index;
1468 }
1469
1470 public int characteristics() {
1471 return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED;
1472 }
1473 }
1474 }