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