Vector - Notes By ShariqSP

Vector in Java

Vector is a resizable array implementation of the List interface in Java. It is similar to ArrayList but provides synchronized operations, making it thread-safe.

Definition:

A Vector is a collection that maintains an underlying array to store elements. It dynamically grows and shrinks as elements are added or removed, similar to an ArrayList. However, unlike ArrayList, Vector is synchronized, meaning it is thread-safe for use in multi-threaded environments.

Methods:

  • add(E element): Adds the specified element to the end of the vector.
  • add(int index, E element): Inserts the specified element at the specified position in the vector.
  • remove(int index): Removes the element at the specified position in the vector.
  • get(int index): Returns the element at the specified position in the vector.
  • size(): Returns the number of elements in the vector.
  • isEmpty(): Returns true if the vector contains no elements.
  • contains(Object obj): Returns true if the vector contains the specified element.
  • clear(): Removes all elements from the vector.

Example:


            import java.util.Vector;
            
            public class Main {
                public static void main(String[] args) {
                    // Create a Vector of integers
                    Vector vector = new Vector<>();
            
                    // Add elements to the vector
                    vector.add(10);
                    vector.add(20);
                    vector.add(30);
            
                    // Display the elements
                    System.out.println("Elements in the vector:");
                    System.out.println(vector);
            
                    // Remove an element
                    vector.remove(1);
            
                    // Display the modified vector
                    System.out.println("\nElements after removal:");
                    System.out.println(vector);
                }
            }
                

In this example, a Vector is created to store integers. Elements are added to the vector using the add() method and removed using the remove() method.

Advantages:

Disadvantages:

Interview Questions and MCQs on Vector in Java

Interview Questions:

  1. What is a Vector in Java?
  2. How is Vector different from ArrayList?
  3. What are the key features of Vector?
  4. How is capacity increased in Vector?
  5. What are the synchronization features of Vector?
  6. What is the role of the synchronized keyword in Vector?
  7. How do you add elements to a Vector?
  8. How do you remove elements from a Vector?
  9. Explain the concept of resizing in Vector.
  10. What are the advantages and disadvantages of using Vector?

Multiple Choice Questions (MCQs):

  1. Which package is required to use Vector in Java?
    a) java.util
    b) java.lang
    c) java.util.collection
    d) java.util.vector
    Answer: a) java.util
  2. Which of the following is true about Vector?
    a) It is synchronized by default
    b) It is not synchronized by default
    c) It can't contain null elements
    d) None of the above
    Answer: a) It is synchronized by default
  3. Which method is used to add an element to the end of a Vector?
    a) addLast()
    b) add()
    c) append()
    d) addToEnd()
    Answer: b) add()
  4. Which method is used to remove an element from a Vector at a specific index?
    a) remove(index)
    b) removeElement(index)
    c) delete(index)
    d) removeAt(index)
    Answer: a) remove(index)
  5. Which interface does Vector implement?
    a) List
    b) Collection
    c) Iterable
    d) All of the above
    Answer: d) All of the above