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