1. /*
  2. * @(#)Vector.java 1.96 04/02/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util;
  8. /**
  9. * The <code>Vector</code> class implements a growable array of
  10. * objects. Like an array, it contains components that can be
  11. * accessed using an integer index. However, the size of a
  12. * <code>Vector</code> can grow or shrink as needed to accommodate
  13. * adding and removing items after the <code>Vector</code> has been created.<p>
  14. *
  15. * Each vector tries to optimize storage management by maintaining a
  16. * <code>capacity</code> and a <code>capacityIncrement</code>. The
  17. * <code>capacity</code> is always at least as large as the vector
  18. * size; it is usually larger because as components are added to the
  19. * vector, the vector's storage increases in chunks the size of
  20. * <code>capacityIncrement</code>. An application can increase the
  21. * capacity of a vector before inserting a large number of
  22. * components; this reduces the amount of incremental reallocation. <p>
  23. *
  24. * As of the Java 2 platform v1.2, this class has been retrofitted to
  25. * implement List, so that it becomes a part of Java's collection framework.
  26. * Unlike the new collection implementations, Vector is synchronized.<p>
  27. *
  28. * The Iterators returned by Vector's iterator and listIterator
  29. * methods are <em>fail-fast</em>: if the Vector is structurally modified
  30. * at any time after the Iterator is created, in any way except through the
  31. * Iterator's own remove or add methods, the Iterator will throw a
  32. * ConcurrentModificationException. Thus, in the face of concurrent
  33. * modification, the Iterator fails quickly and cleanly, rather than risking
  34. * arbitrary, non-deterministic behavior at an undetermined time in the future.
  35. * The Enumerations returned by Vector's elements method are <em>not</em>
  36. * fail-fast.
  37. *
  38. * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
  39. * as it is, generally speaking, impossible to make any hard guarantees in the
  40. * presence of unsynchronized concurrent modification. Fail-fast iterators
  41. * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
  42. * Therefore, it would be wrong to write a program that depended on this
  43. * exception for its correctness: <i>the fail-fast behavior of iterators
  44. * should be used only to detect bugs.</i><p>
  45. *
  46. * This class is a member of the
  47. * <a href="{@docRoot}/../guide/collections/index.html">
  48. * Java Collections Framework</a>.
  49. *
  50. * @author Lee Boynton
  51. * @author Jonathan Payne
  52. * @version 1.96, 02/19/04
  53. * @see Collection
  54. * @see List
  55. * @see ArrayList
  56. * @see LinkedList
  57. * @since JDK1.0
  58. */
  59. public class Vector<E>
  60. extends AbstractList<E>
  61. implements List<E>, RandomAccess, Cloneable, java.io.Serializable
  62. {
  63. /**
  64. * The array buffer into which the components of the vector are
  65. * stored. The capacity of the vector is the length of this array buffer,
  66. * and is at least large enough to contain all the vector's elements.<p>
  67. *
  68. * Any array elements following the last element in the Vector are null.
  69. *
  70. * @serial
  71. */
  72. protected Object[] elementData;
  73. /**
  74. * The number of valid components in this <tt>Vector</tt> object.
  75. * Components <tt>elementData[0]</tt> through
  76. * <tt>elementData[elementCount-1]</tt> are the actual items.
  77. *
  78. * @serial
  79. */
  80. protected int elementCount;
  81. /**
  82. * The amount by which the capacity of the vector is automatically
  83. * incremented when its size becomes greater than its capacity. If
  84. * the capacity increment is less than or equal to zero, the capacity
  85. * of the vector is doubled each time it needs to grow.
  86. *
  87. * @serial
  88. */
  89. protected int capacityIncrement;
  90. /** use serialVersionUID from JDK 1.0.2 for interoperability */
  91. private static final long serialVersionUID = -2767605614048989439L;
  92. /**
  93. * Constructs an empty vector with the specified initial capacity and
  94. * capacity increment.
  95. *
  96. * @param initialCapacity the initial capacity of the vector.
  97. * @param capacityIncrement the amount by which the capacity is
  98. * increased when the vector overflows.
  99. * @exception IllegalArgumentException if the specified initial capacity
  100. * is negative
  101. */
  102. public Vector(int initialCapacity, int capacityIncrement) {
  103. super();
  104. if (initialCapacity < 0)
  105. throw new IllegalArgumentException("Illegal Capacity: "+
  106. initialCapacity);
  107. this.elementData = new Object[initialCapacity];
  108. this.capacityIncrement = capacityIncrement;
  109. }
  110. /**
  111. * Constructs an empty vector with the specified initial capacity and
  112. * with its capacity increment equal to zero.
  113. *
  114. * @param initialCapacity the initial capacity of the vector.
  115. * @exception IllegalArgumentException if the specified initial capacity
  116. * is negative
  117. */
  118. public Vector(int initialCapacity) {
  119. this(initialCapacity, 0);
  120. }
  121. /**
  122. * Constructs an empty vector so that its internal data array
  123. * has size <tt>10</tt> and its standard capacity increment is
  124. * zero.
  125. */
  126. public Vector() {
  127. this(10);
  128. }
  129. /**
  130. * Constructs a vector containing the elements of the specified
  131. * collection, in the order they are returned by the collection's
  132. * iterator.
  133. *
  134. * @param c the collection whose elements are to be placed into this
  135. * vector.
  136. * @throws NullPointerException if the specified collection is null.
  137. * @since 1.2
  138. */
  139. public Vector(Collection<? extends E> c) {
  140. elementCount = c.size();
  141. // 10% for growth
  142. elementData = new Object[
  143. (int)Math.min((elementCount*110L)/100,Integer.MAX_VALUE)];
  144. c.toArray(elementData);
  145. }
  146. /**
  147. * Copies the components of this vector into the specified array. The
  148. * item at index <tt>k</tt> in this vector is copied into component
  149. * <tt>k</tt> of <tt>anArray</tt>. The array must be big enough to hold
  150. * all the objects in this vector, else an
  151. * <tt>IndexOutOfBoundsException</tt> is thrown.
  152. *
  153. * @param anArray the array into which the components get copied.
  154. * @throws NullPointerException if the given array is null.
  155. */
  156. public synchronized void copyInto(Object[] anArray) {
  157. System.arraycopy(elementData, 0, anArray, 0, elementCount);
  158. }
  159. /**
  160. * Trims the capacity of this vector to be the vector's current
  161. * size. If the capacity of this vector is larger than its current
  162. * size, then the capacity is changed to equal the size by replacing
  163. * its internal data array, kept in the field <tt>elementData</tt>,
  164. * with a smaller one. An application can use this operation to
  165. * minimize the storage of a vector.
  166. */
  167. public synchronized void trimToSize() {
  168. modCount++;
  169. int oldCapacity = elementData.length;
  170. if (elementCount < oldCapacity) {
  171. Object oldData[] = elementData;
  172. elementData = new Object[elementCount];
  173. System.arraycopy(oldData, 0, elementData, 0, elementCount);
  174. }
  175. }
  176. /**
  177. * Increases the capacity of this vector, if necessary, to ensure
  178. * that it can hold at least the number of components specified by
  179. * the minimum capacity argument.
  180. *
  181. * <p>If the current capacity of this vector is less than
  182. * <tt>minCapacity</tt>, then its capacity is increased by replacing its
  183. * internal data array, kept in the field <tt>elementData</tt>, with a
  184. * larger one. The size of the new data array will be the old size plus
  185. * <tt>capacityIncrement</tt>, unless the value of
  186. * <tt>capacityIncrement</tt> is less than or equal to zero, in which case
  187. * the new capacity will be twice the old capacity; but if this new size
  188. * is still smaller than <tt>minCapacity</tt>, then the new capacity will
  189. * be <tt>minCapacity</tt>.
  190. *
  191. * @param minCapacity the desired minimum capacity.
  192. */
  193. public synchronized void ensureCapacity(int minCapacity) {
  194. modCount++;
  195. ensureCapacityHelper(minCapacity);
  196. }
  197. /**
  198. * This implements the unsynchronized semantics of ensureCapacity.
  199. * Synchronized methods in this class can internally call this
  200. * method for ensuring capacity without incurring the cost of an
  201. * extra synchronization.
  202. *
  203. * @see java.util.Vector#ensureCapacity(int)
  204. */
  205. private void ensureCapacityHelper(int minCapacity) {
  206. int oldCapacity = elementData.length;
  207. if (minCapacity > oldCapacity) {
  208. Object[] oldData = elementData;
  209. int newCapacity = (capacityIncrement > 0) ?
  210. (oldCapacity + capacityIncrement) : (oldCapacity * 2);
  211. if (newCapacity < minCapacity) {
  212. newCapacity = minCapacity;
  213. }
  214. elementData = new Object[newCapacity];
  215. System.arraycopy(oldData, 0, elementData, 0, elementCount);
  216. }
  217. }
  218. /**
  219. * Sets the size of this vector. If the new size is greater than the
  220. * current size, new <code>null</code> items are added to the end of
  221. * the vector. If the new size is less than the current size, all
  222. * components at index <code>newSize</code> and greater are discarded.
  223. *
  224. * @param newSize the new size of this vector.
  225. * @throws ArrayIndexOutOfBoundsException if new size is negative.
  226. */
  227. public synchronized void setSize(int newSize) {
  228. modCount++;
  229. if (newSize > elementCount) {
  230. ensureCapacityHelper(newSize);
  231. } else {
  232. for (int i = newSize ; i < elementCount ; i++) {
  233. elementData[i] = null;
  234. }
  235. }
  236. elementCount = newSize;
  237. }
  238. /**
  239. * Returns the current capacity of this vector.
  240. *
  241. * @return the current capacity (the length of its internal
  242. * data array, kept in the field <tt>elementData</tt>
  243. * of this vector).
  244. */
  245. public synchronized int capacity() {
  246. return elementData.length;
  247. }
  248. /**
  249. * Returns the number of components in this vector.
  250. *
  251. * @return the number of components in this vector.
  252. */
  253. public synchronized int size() {
  254. return elementCount;
  255. }
  256. /**
  257. * Tests if this vector has no components.
  258. *
  259. * @return <code>true</code> if and only if this vector has
  260. * no components, that is, its size is zero;
  261. * <code>false</code> otherwise.
  262. */
  263. public synchronized boolean isEmpty() {
  264. return elementCount == 0;
  265. }
  266. /**
  267. * Returns an enumeration of the components of this vector. The
  268. * returned <tt>Enumeration</tt> object will generate all items in
  269. * this vector. The first item generated is the item at index <tt>0</tt>,
  270. * then the item at index <tt>1</tt>, and so on.
  271. *
  272. * @return an enumeration of the components of this vector.
  273. * @see Enumeration
  274. * @see Iterator
  275. */
  276. public Enumeration<E> elements() {
  277. return new Enumeration<E>() {
  278. int count = 0;
  279. public boolean hasMoreElements() {
  280. return count < elementCount;
  281. }
  282. public E nextElement() {
  283. synchronized (Vector.this) {
  284. if (count < elementCount) {
  285. return (E)elementData[count++];
  286. }
  287. }
  288. throw new NoSuchElementException("Vector Enumeration");
  289. }
  290. };
  291. }
  292. /**
  293. * Tests if the specified object is a component in this vector.
  294. *
  295. * @param elem an object.
  296. * @return <code>true</code> if and only if the specified object
  297. * is the same as a component in this vector, as determined by the
  298. * <tt>equals</tt> method; <code>false</code> otherwise.
  299. */
  300. public boolean contains(Object elem) {
  301. return indexOf(elem, 0) >= 0;
  302. }
  303. /**
  304. * Searches for the first occurence of the given argument, testing
  305. * for equality using the <code>equals</code> method.
  306. *
  307. * @param elem an object.
  308. * @return the index of the first occurrence of the argument in this
  309. * vector, that is, the smallest value <tt>k</tt> such that
  310. * <tt>elem.equals(elementData[k])</tt> is <tt>true</tt>
  311. * returns <code>-1</code> if the object is not found.
  312. * @see Object#equals(Object)
  313. */
  314. public int indexOf(Object elem) {
  315. return indexOf(elem, 0);
  316. }
  317. /**
  318. * Searches for the first occurence of the given argument, beginning
  319. * the search at <code>index</code>, and testing for equality using
  320. * the <code>equals</code> method.
  321. *
  322. * @param elem an object.
  323. * @param index the non-negative index to start searching from.
  324. * @return the index of the first occurrence of the object argument in
  325. * this vector at position <code>index</code> or later in the
  326. * vector, that is, the smallest value <tt>k</tt> such that
  327. * <tt>elem.equals(elementData[k]) && (k >= index)</tt> is
  328. * <tt>true</tt> returns <code>-1</code> if the object is not
  329. * found. (Returns <code>-1</code> if <tt>index</tt> >= the
  330. * current size of this <tt>Vector</tt>.)
  331. * @exception IndexOutOfBoundsException if <tt>index</tt> is negative.
  332. * @see Object#equals(Object)
  333. */
  334. public synchronized int indexOf(Object elem, int index) {
  335. if (elem == null) {
  336. for (int i = index ; i < elementCount ; i++)
  337. if (elementData[i]==null)
  338. return i;
  339. } else {
  340. for (int i = index ; i < elementCount ; i++)
  341. if (elem.equals(elementData[i]))
  342. return i;
  343. }
  344. return -1;
  345. }
  346. /**
  347. * Returns the index of the last occurrence of the specified object in
  348. * this vector.
  349. *
  350. * @param elem the desired component.
  351. * @return the index of the last occurrence of the specified object in
  352. * this vector, that is, the largest value <tt>k</tt> such that
  353. * <tt>elem.equals(elementData[k])</tt> is <tt>true</tt>
  354. * returns <code>-1</code> if the object is not found.
  355. */
  356. public synchronized int lastIndexOf(Object elem) {
  357. return lastIndexOf(elem, elementCount-1);
  358. }
  359. /**
  360. * Searches backwards for the specified object, starting from the
  361. * specified index, and returns an index to it.
  362. *
  363. * @param elem the desired component.
  364. * @param index the index to start searching from.
  365. * @return the index of the last occurrence of the specified object in this
  366. * vector at position less than or equal to <code>index</code> in
  367. * the vector, that is, the largest value <tt>k</tt> such that
  368. * <tt>elem.equals(elementData[k]) && (k <= index)</tt> is
  369. * <tt>true</tt> <code>-1</code> if the object is not found.
  370. * (Returns <code>-1</code> if <tt>index</tt> is negative.)
  371. * @exception IndexOutOfBoundsException if <tt>index</tt> is greater
  372. * than or equal to the current size of this vector.
  373. */
  374. public synchronized int lastIndexOf(Object elem, int index) {
  375. if (index >= elementCount)
  376. throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
  377. if (elem == null) {
  378. for (int i = index; i >= 0; i--)
  379. if (elementData[i]==null)
  380. return i;
  381. } else {
  382. for (int i = index; i >= 0; i--)
  383. if (elem.equals(elementData[i]))
  384. return i;
  385. }
  386. return -1;
  387. }
  388. /**
  389. * Returns the component at the specified index.<p>
  390. *
  391. * This method is identical in functionality to the get method
  392. * (which is part of the List interface).
  393. *
  394. * @param index an index into this vector.
  395. * @return the component at the specified index.
  396. * @exception ArrayIndexOutOfBoundsException if the <tt>index</tt>
  397. * is negative or not less than the current size of this
  398. * <tt>Vector</tt> object.
  399. * given.
  400. * @see #get(int)
  401. * @see List
  402. */
  403. public synchronized E elementAt(int index) {
  404. if (index >= elementCount) {
  405. throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
  406. }
  407. return (E)elementData[index];
  408. }
  409. /**
  410. * Returns the first component (the item at index <tt>0</tt>) of
  411. * this vector.
  412. *
  413. * @return the first component of this vector.
  414. * @exception NoSuchElementException if this vector has no components.
  415. */
  416. public synchronized E firstElement() {
  417. if (elementCount == 0) {
  418. throw new NoSuchElementException();
  419. }
  420. return (E)elementData[0];
  421. }
  422. /**
  423. * Returns the last component of the vector.
  424. *
  425. * @return the last component of the vector, i.e., the component at index
  426. * <code>size() - 1</code>.
  427. * @exception NoSuchElementException if this vector is empty.
  428. */
  429. public synchronized E lastElement() {
  430. if (elementCount == 0) {
  431. throw new NoSuchElementException();
  432. }
  433. return (E)elementData[elementCount - 1];
  434. }
  435. /**
  436. * Sets the component at the specified <code>index</code> of this
  437. * vector to be the specified object. The previous component at that
  438. * position is discarded.<p>
  439. *
  440. * The index must be a value greater than or equal to <code>0</code>
  441. * and less than the current size of the vector. <p>
  442. *
  443. * This method is identical in functionality to the set method
  444. * (which is part of the List interface). Note that the set method reverses
  445. * the order of the parameters, to more closely match array usage. Note
  446. * also that the set method returns the old value that was stored at the
  447. * specified position.
  448. *
  449. * @param obj what the component is to be set to.
  450. * @param index the specified index.
  451. * @exception ArrayIndexOutOfBoundsException if the index was invalid.
  452. * @see #size()
  453. * @see List
  454. * @see #set(int, java.lang.Object)
  455. */
  456. public synchronized void setElementAt(E obj, int index) {
  457. if (index >= elementCount) {
  458. throw new ArrayIndexOutOfBoundsException(index + " >= " +
  459. elementCount);
  460. }
  461. elementData[index] = obj;
  462. }
  463. /**
  464. * Deletes the component at the specified index. Each component in
  465. * this vector with an index greater or equal to the specified
  466. * <code>index</code> is shifted downward to have an index one
  467. * smaller than the value it had previously. The size of this vector
  468. * is decreased by <tt>1</tt>.<p>
  469. *
  470. * The index must be a value greater than or equal to <code>0</code>
  471. * and less than the current size of the vector. <p>
  472. *
  473. * This method is identical in functionality to the remove method
  474. * (which is part of the List interface). Note that the remove method
  475. * returns the old value that was stored at the specified position.
  476. *
  477. * @param index the index of the object to remove.
  478. * @exception ArrayIndexOutOfBoundsException if the index was invalid.
  479. * @see #size()
  480. * @see #remove(int)
  481. * @see List
  482. */
  483. public synchronized void removeElementAt(int index) {
  484. modCount++;
  485. if (index >= elementCount) {
  486. throw new ArrayIndexOutOfBoundsException(index + " >= " +
  487. elementCount);
  488. }
  489. else if (index < 0) {
  490. throw new ArrayIndexOutOfBoundsException(index);
  491. }
  492. int j = elementCount - index - 1;
  493. if (j > 0) {
  494. System.arraycopy(elementData, index + 1, elementData, index, j);
  495. }
  496. elementCount--;
  497. elementData[elementCount] = null; /* to let gc do its work */
  498. }
  499. /**
  500. * Inserts the specified object as a component in this vector at the
  501. * specified <code>index</code>. Each component in this vector with
  502. * an index greater or equal to the specified <code>index</code> is
  503. * shifted upward to have an index one greater than the value it had
  504. * previously. <p>
  505. *
  506. * The index must be a value greater than or equal to <code>0</code>
  507. * and less than or equal to the current size of the vector. (If the
  508. * index is equal to the current size of the vector, the new element
  509. * is appended to the Vector.)<p>
  510. *
  511. * This method is identical in functionality to the add(Object, int) method
  512. * (which is part of the List interface). Note that the add method reverses
  513. * the order of the parameters, to more closely match array usage.
  514. *
  515. * @param obj the component to insert.
  516. * @param index where to insert the new component.
  517. * @exception ArrayIndexOutOfBoundsException if the index was invalid.
  518. * @see #size()
  519. * @see #add(int, Object)
  520. * @see List
  521. */
  522. public synchronized void insertElementAt(E obj, int index) {
  523. modCount++;
  524. if (index > elementCount) {
  525. throw new ArrayIndexOutOfBoundsException(index
  526. + " > " + elementCount);
  527. }
  528. ensureCapacityHelper(elementCount + 1);
  529. System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
  530. elementData[index] = obj;
  531. elementCount++;
  532. }
  533. /**
  534. * Adds the specified component to the end of this vector,
  535. * increasing its size by one. The capacity of this vector is
  536. * increased if its size becomes greater than its capacity. <p>
  537. *
  538. * This method is identical in functionality to the add(Object) method
  539. * (which is part of the List interface).
  540. *
  541. * @param obj the component to be added.
  542. * @see #add(Object)
  543. * @see List
  544. */
  545. public synchronized void addElement(E obj) {
  546. modCount++;
  547. ensureCapacityHelper(elementCount + 1);
  548. elementData[elementCount++] = obj;
  549. }
  550. /**
  551. * Removes the first (lowest-indexed) occurrence of the argument
  552. * from this vector. If the object is found in this vector, each
  553. * component in the vector with an index greater or equal to the
  554. * object's index is shifted downward to have an index one smaller
  555. * than the value it had previously.<p>
  556. *
  557. * This method is identical in functionality to the remove(Object)
  558. * method (which is part of the List interface).
  559. *
  560. * @param obj the component to be removed.
  561. * @return <code>true</code> if the argument was a component of this
  562. * vector; <code>false</code> otherwise.
  563. * @see List#remove(Object)
  564. * @see List
  565. */
  566. public synchronized boolean removeElement(Object obj) {
  567. modCount++;
  568. int i = indexOf(obj);
  569. if (i >= 0) {
  570. removeElementAt(i);
  571. return true;
  572. }
  573. return false;
  574. }
  575. /**
  576. * Removes all components from this vector and sets its size to zero.<p>
  577. *
  578. * This method is identical in functionality to the clear method
  579. * (which is part of the List interface).
  580. *
  581. * @see #clear
  582. * @see List
  583. */
  584. public synchronized void removeAllElements() {
  585. modCount++;
  586. // Let gc do its work
  587. for (int i = 0; i < elementCount; i++)
  588. elementData[i] = null;
  589. elementCount = 0;
  590. }
  591. /**
  592. * Returns a clone of this vector. The copy will contain a
  593. * reference to a clone of the internal data array, not a reference
  594. * to the original internal data array of this <tt>Vector</tt> object.
  595. *
  596. * @return a clone of this vector.
  597. */
  598. public synchronized Object clone() {
  599. try {
  600. Vector<E> v = (Vector<E>) super.clone();
  601. v.elementData = new Object[elementCount];
  602. System.arraycopy(elementData, 0, v.elementData, 0, elementCount);
  603. v.modCount = 0;
  604. return v;
  605. } catch (CloneNotSupportedException e) {
  606. // this shouldn't happen, since we are Cloneable
  607. throw new InternalError();
  608. }
  609. }
  610. /**
  611. * Returns an array containing all of the elements in this Vector
  612. * in the correct order.
  613. *
  614. * @since 1.2
  615. */
  616. public synchronized Object[] toArray() {
  617. Object[] result = new Object[elementCount];
  618. System.arraycopy(elementData, 0, result, 0, elementCount);
  619. return result;
  620. }
  621. /**
  622. * Returns an array containing all of the elements in this Vector in the
  623. * correct order; the runtime type of the returned array is that of the
  624. * specified array. If the Vector fits in the specified array, it is
  625. * returned therein. Otherwise, a new array is allocated with the runtime
  626. * type of the specified array and the size of this Vector.<p>
  627. *
  628. * If the Vector fits in the specified array with room to spare
  629. * (i.e., the array has more elements than the Vector),
  630. * the element in the array immediately following the end of the
  631. * Vector is set to null. This is useful in determining the length
  632. * of the Vector <em>only</em> if the caller knows that the Vector
  633. * does not contain any null elements.
  634. *
  635. * @param a the array into which the elements of the Vector are to
  636. * be stored, if it is big enough; otherwise, a new array of the
  637. * same runtime type is allocated for this purpose.
  638. * @return an array containing the elements of the Vector.
  639. * @exception ArrayStoreException the runtime type of a is not a supertype
  640. * of the runtime type of every element in this Vector.
  641. * @throws NullPointerException if the given array is null.
  642. * @since 1.2
  643. */
  644. public synchronized <T> T[] toArray(T[] a) {
  645. if (a.length < elementCount)
  646. a = (T[])java.lang.reflect.Array.newInstance(
  647. a.getClass().getComponentType(), elementCount);
  648. System.arraycopy(elementData, 0, a, 0, elementCount);
  649. if (a.length > elementCount)
  650. a[elementCount] = null;
  651. return a;
  652. }
  653. // Positional Access Operations
  654. /**
  655. * Returns the element at the specified position in this Vector.
  656. *
  657. * @param index index of element to return.
  658. * @return object at the specified index
  659. * @exception ArrayIndexOutOfBoundsException index is out of range (index
  660. * < 0 || index >= size()).
  661. * @since 1.2
  662. */
  663. public synchronized E get(int index) {
  664. if (index >= elementCount)
  665. throw new ArrayIndexOutOfBoundsException(index);
  666. return (E)elementData[index];
  667. }
  668. /**
  669. * Replaces the element at the specified position in this Vector with the
  670. * specified element.
  671. *
  672. * @param index index of element to replace.
  673. * @param element element to be stored at the specified position.
  674. * @return the element previously at the specified position.
  675. * @exception ArrayIndexOutOfBoundsException index out of range
  676. * (index < 0 || index >= size()).
  677. * @since 1.2
  678. */
  679. public synchronized E set(int index, E element) {
  680. if (index >= elementCount)
  681. throw new ArrayIndexOutOfBoundsException(index);
  682. Object oldValue = elementData[index];
  683. elementData[index] = element;
  684. return (E)oldValue;
  685. }
  686. /**
  687. * Appends the specified element to the end of this Vector.
  688. *
  689. * @param o element to be appended to this Vector.
  690. * @return true (as per the general contract of Collection.add).
  691. * @since 1.2
  692. */
  693. public synchronized boolean add(E o) {
  694. modCount++;
  695. ensureCapacityHelper(elementCount + 1);
  696. elementData[elementCount++] = o;
  697. return true;
  698. }
  699. /**
  700. * Removes the first occurrence of the specified element in this Vector
  701. * If the Vector does not contain the element, it is unchanged. More
  702. * formally, removes the element with the lowest index i such that
  703. * <code>(o==null ? get(i)==null : o.equals(get(i)))</code> (if such
  704. * an element exists).
  705. *
  706. * @param o element to be removed from this Vector, if present.
  707. * @return true if the Vector contained the specified element.
  708. * @since 1.2
  709. */
  710. public boolean remove(Object o) {
  711. return removeElement(o);
  712. }
  713. /**
  714. * Inserts the specified element at the specified position in this Vector.
  715. * Shifts the element currently at that position (if any) and any
  716. * subsequent elements to the right (adds one to their indices).
  717. *
  718. * @param index index at which the specified element is to be inserted.
  719. * @param element element to be inserted.
  720. * @exception ArrayIndexOutOfBoundsException index is out of range
  721. * (index < 0 || index > size()).
  722. * @since 1.2
  723. */
  724. public void add(int index, E element) {
  725. insertElementAt(element, index);
  726. }
  727. /**
  728. * Removes the element at the specified position in this Vector.
  729. * shifts any subsequent elements to the left (subtracts one from their
  730. * indices). Returns the element that was removed from the Vector.
  731. *
  732. * @exception ArrayIndexOutOfBoundsException index out of range (index
  733. * < 0 || index >= size()).
  734. * @param index the index of the element to removed.
  735. * @return element that was removed
  736. * @since 1.2
  737. */
  738. public synchronized E remove(int index) {
  739. modCount++;
  740. if (index >= elementCount)
  741. throw new ArrayIndexOutOfBoundsException(index);
  742. Object oldValue = elementData[index];
  743. int numMoved = elementCount - index - 1;
  744. if (numMoved > 0)
  745. System.arraycopy(elementData, index+1, elementData, index,
  746. numMoved);
  747. elementData[--elementCount] = null; // Let gc do its work
  748. return (E)oldValue;
  749. }
  750. /**
  751. * Removes all of the elements from this Vector. The Vector will
  752. * be empty after this call returns (unless it throws an exception).
  753. *
  754. * @since 1.2
  755. */
  756. public void clear() {
  757. removeAllElements();
  758. }
  759. // Bulk Operations
  760. /**
  761. * Returns true if this Vector contains all of the elements in the
  762. * specified Collection.
  763. *
  764. * @param c a collection whose elements will be tested for containment
  765. * in this Vector
  766. * @return true if this Vector contains all of the elements in the
  767. * specified collection.
  768. * @throws NullPointerException if the specified collection is null.
  769. */
  770. public synchronized boolean containsAll(Collection<?> c) {
  771. return super.containsAll(c);
  772. }
  773. /**
  774. * Appends all of the elements in the specified Collection to the end of
  775. * this Vector, in the order that they are returned by the specified
  776. * Collection's Iterator. The behavior of this operation is undefined if
  777. * the specified Collection is modified while the operation is in progress.
  778. * (This implies that the behavior of this call is undefined if the
  779. * specified Collection is this Vector, and this Vector is nonempty.)
  780. *
  781. * @param c elements to be inserted into this Vector.
  782. * @return <tt>true</tt> if this Vector changed as a result of the call.
  783. * @throws NullPointerException if the specified collection is null.
  784. * @since 1.2
  785. */
  786. public synchronized boolean addAll(Collection<? extends E> c) {
  787. modCount++;
  788. Object[] a = c.toArray();
  789. int numNew = a.length;
  790. ensureCapacityHelper(elementCount + numNew);
  791. System.arraycopy(a, 0, elementData, elementCount, numNew);
  792. elementCount += numNew;
  793. return numNew != 0;
  794. }
  795. /**
  796. * Removes from this Vector all of its elements that are contained in the
  797. * specified Collection.
  798. *
  799. * @param c a collection of elements to be removed from the Vector
  800. * @return true if this Vector changed as a result of the call.
  801. * @throws NullPointerException if the specified collection is null.
  802. * @since 1.2
  803. */
  804. public synchronized boolean removeAll(Collection<?> c) {
  805. return super.removeAll(c);
  806. }
  807. /**
  808. * Retains only the elements in this Vector that are contained in the
  809. * specified Collection. In other words, removes from this Vector all
  810. * of its elements that are not contained in the specified Collection.
  811. *
  812. * @param c a collection of elements to be retained in this Vector
  813. * (all other elements are removed)
  814. * @return true if this Vector changed as a result of the call.
  815. * @throws NullPointerException if the specified collection is null.
  816. * @since 1.2
  817. */
  818. public synchronized boolean retainAll(Collection<?> c) {
  819. return super.retainAll(c);
  820. }
  821. /**
  822. * Inserts all of the elements in the specified Collection into this
  823. * Vector at the specified position. Shifts the element currently at
  824. * that position (if any) and any subsequent elements to the right
  825. * (increases their indices). The new elements will appear in the Vector
  826. * in the order that they are returned by the specified Collection's
  827. * iterator.
  828. *
  829. * @param index index at which to insert first element
  830. * from the specified collection.
  831. * @param c elements to be inserted into this Vector.
  832. * @return <tt>true</tt> if this Vector changed as a result of the call.
  833. * @exception ArrayIndexOutOfBoundsException index out of range (index
  834. * < 0 || index > size()).
  835. * @throws NullPointerException if the specified collection is null.
  836. * @since 1.2
  837. */
  838. public synchronized boolean addAll(int index, Collection<? extends E> c) {
  839. modCount++;
  840. if (index < 0 || index > elementCount)
  841. throw new ArrayIndexOutOfBoundsException(index);
  842. Object[] a = c.toArray();
  843. int numNew = a.length;
  844. ensureCapacityHelper(elementCount + numNew);
  845. int numMoved = elementCount - index;
  846. if (numMoved > 0)
  847. System.arraycopy(elementData, index, elementData, index + numNew,
  848. numMoved);
  849. System.arraycopy(a, 0, elementData, index, numNew);
  850. elementCount += numNew;
  851. return numNew != 0;
  852. }
  853. /**
  854. * Compares the specified Object with this Vector for equality. Returns
  855. * true if and only if the specified Object is also a List, both Lists
  856. * have the same size, and all corresponding pairs of elements in the two
  857. * Lists are <em>equal</em>. (Two elements <code>e1</code> and
  858. * <code>e2</code> are <em>equal</em> if <code>(e1==null ? e2==null :
  859. * e1.equals(e2))</code>.) In other words, two Lists are defined to be
  860. * equal if they contain the same elements in the same order.
  861. *
  862. * @param o the Object to be compared for equality with this Vector.
  863. * @return true if the specified Object is equal to this Vector
  864. */
  865. public synchronized boolean equals(Object o) {
  866. return super.equals(o);
  867. }
  868. /**
  869. * Returns the hash code value for this Vector.
  870. */
  871. public synchronized int hashCode() {
  872. return super.hashCode();
  873. }
  874. /**
  875. * Returns a string representation of this Vector, containing
  876. * the String representation of each element.
  877. */
  878. public synchronized String toString() {
  879. return super.toString();
  880. }
  881. /**
  882. * Returns a view of the portion of this List between fromIndex,
  883. * inclusive, and toIndex, exclusive. (If fromIndex and ToIndex are
  884. * equal, the returned List is empty.) The returned List is backed by this
  885. * List, so changes in the returned List are reflected in this List, and
  886. * vice-versa. The returned List supports all of the optional List
  887. * operations supported by this List.<p>
  888. *
  889. * This method eliminates the need for explicit range operations (of
  890. * the sort that commonly exist for arrays). Any operation that expects
  891. * a List can be used as a range operation by operating on a subList view
  892. * instead of a whole List. For example, the following idiom
  893. * removes a range of elements from a List:
  894. * <pre>
  895. * list.subList(from, to).clear();
  896. * </pre>
  897. * Similar idioms may be constructed for indexOf and lastIndexOf,
  898. * and all of the algorithms in the Collections class can be applied to
  899. * a subList.<p>
  900. *
  901. * The semantics of the List returned by this method become undefined if
  902. * the backing list (i.e., this List) is <i>structurally modified</i> in
  903. * any way other than via the returned List. (Structural modifications are
  904. * those that change the size of the List, or otherwise perturb it in such
  905. * a fashion that iterations in progress may yield incorrect results.)
  906. *
  907. * @param fromIndex low endpoint (inclusive) of the subList.
  908. * @param toIndex high endpoint (exclusive) of the subList.
  909. * @return a view of the specified range within this List.
  910. * @throws IndexOutOfBoundsException endpoint index value out of range
  911. * <code>(fromIndex < 0 || toIndex > size)</code>
  912. * @throws IllegalArgumentException endpoint indices out of order
  913. * <code>(fromIndex > toIndex)</code>
  914. */
  915. public synchronized List<E> subList(int fromIndex, int toIndex) {
  916. return Collections.synchronizedList(super.subList(fromIndex, toIndex),
  917. this);
  918. }
  919. /**
  920. * Removes from this List all of the elements whose index is between
  921. * fromIndex, inclusive and toIndex, exclusive. Shifts any succeeding
  922. * elements to the left (reduces their index).
  923. * This call shortens the ArrayList by (toIndex - fromIndex) elements. (If
  924. * toIndex==fromIndex, this operation has no effect.)
  925. *
  926. * @param fromIndex index of first element to be removed.
  927. * @param toIndex index after last element to be removed.
  928. */
  929. protected synchronized void removeRange(int fromIndex, int toIndex) {
  930. modCount++;
  931. int numMoved = elementCount - toIndex;
  932. System.arraycopy(elementData, toIndex, elementData, fromIndex,
  933. numMoved);
  934. // Let gc do its work
  935. int newElementCount = elementCount - (toIndex-fromIndex);
  936. while (elementCount != newElementCount)
  937. elementData[--elementCount] = null;
  938. }
  939. /**
  940. * Save the state of the <tt>Vector</tt> instance to a stream (that
  941. * is, serialize it). This method is present merely for synchronization.
  942. * It just calls the default readObject method.
  943. */
  944. private synchronized void writeObject(java.io.ObjectOutputStream s)
  945. throws java.io.IOException
  946. {
  947. s.defaultWriteObject();
  948. }
  949. }