Vector methods in Java
1. Adding Elements
● add(E e) – Appends the specified element to the end.
● add(int index, E element) – Inserts element at the specified position.
● addAll(Collection c) – Appends all elements from another collection.
● addElement(E obj) – Legacy method; similar to add(E e).
2. Accessing Elements
● get(int index) – Returns the element at the specified position.
● elementAt(int index) – Legacy equivalent of get(int index).
● firstElement() – Returns the first element.
● lastElement() – Returns the last element.
3. Modifying Elements
● set(int index, E element) – Replaces the element at a given index.
● setElementAt(E obj, int index) – Legacy equivalent of set().
● insertElementAt(E obj, int index) – Inserts element at specified position.
4. Removing Elements
● remove(int index) – Removes element at the specified index.
● remove(Object o) – Removes the first occurrence of the object.
● removeAll(Collection c) – Removes all elements from a collection.
● removeAllElements() – Removes all elements (legacy method).
● removeElement(Object obj) – Legacy equivalent of remove(Object o).
● removeElementAt(int index) – Legacy equivalent of remove(int index).
5. Capacity and Size
● size() – Returns the number of elements.
● capacity() – Returns the current capacity of the vector.
● ensureCapacity(int minCapacity) – Increases capacity if needed.
● setSize(int newSize) – Changes the size of the vector.
● trimToSize() – Trims the capacity to match the size.
6. Searching Elements
● contains(Object o) – Checks if the vector contains the specified element.
● indexOf(Object o) – Returns the index of the first occurrence.
● lastIndexOf(Object o) – Returns the index of the last occurrence.
7. Enumeration and Iteration
● elements() – Returns an enumeration of the elements (legacy).
● iterator() – Returns an iterator.
● listIterator() – Returns a list iterator.
8. Utility Methods
● isEmpty() – Checks if the vector is empty.
● clear() – Removes all elements.
● clone() – Returns a shallow copy of the vector.
● toArray() – Converts vector to an array.
● equals(Object o) – Compares the vector to another object.