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