Java Collections – Vector Example

The Vector class is implemented like an array of objects except that it will automatically expand by an incremental capacity when the capacity exceeds the Vector’s capacity. In addition, the Vector has the ability to shrink as needed in the event that elements are removed. In the example I have detailed, I have instantiated it with an initial capacity of 5 and an capacity increment of 4. Once our Vector reaches it’s max capacity it will grow by 4.

Big-O Notation

Vector implementation provides constant-time O(1) performance for the basic operations at the index. Adding and deleting elements at the ends takes O(n). Compared to ArrayList there may be a slight penalty because Vector is synchronized and ArrayList is not.

Creating a Vector

Vector<Car> cars = new Vector<Car>(5,4);

Adding elements

Adding elements to the cars Vector is done by using the add(Object obj) method.

System.out.println("Adding 4 elements to cars Vector()...");
cars.add(prius);
cars.add(pinto);
cars.add(taurus);
cars.add(maxima);

Removing elements

Removing elements is just a matter of calling the overloaded method remove(int index).

cars.remove(2);
cars.remove(3);

You may also remove elements by calling the remove(Object obj) method.

cars.remove(taurus);
cars.remove(camarro);

Size of Collection

Returning the number of elements in a Vector is as easy as calling the size() method.

cars.size();

Iterating through the Collection

Creating an iterator and looping through the collection is quite easy and straight forward.

// Loop through the collection of cars using iterator
ListIterator iter = cars.listIterator();
while (iter.hasNext()) {
  Car c = iter.next();
  System.out.format("%s (%s)n", c.getName(), c.getManufacturer());
}

Iterating through the Collection using foreach

Java 1.5 and above provides a foreach loop, which makes it much easier to iterate over the entire collection. This is my preferred way of doing it.

// Loop through the collection of cars using foreach
for (Car c : cars) {
   System.out.format("%s (%s)n", c.getName(), c.getManufacturer());
}

Full Program Listing (VectorExample.java)

package com.avaldes.tutorials;

import java.util.Vector;

public class VectorExample {

  public static void main(String[] args) {
    Vector<Car> cars = new Vector<Car>(5, 4); 
    System.out.println("Initial cars size: " + cars.size()); 
    System.out.println("Initial cars capacity: " + cars.capacity());
    
    Car prius = new Car("Toyota", "Prius");
    Car pinto = new Car("Ford", "Pinto");
    Car taurus = new Car("Ford", "Taurus");
    Car maxima = new Car("Nissan", "Maxima");
    Car towncar= new Car("Lincoln", "Town Car");
    Car camaro = new Car("Chevrolet", "Carmaro");
    Car etzel = new Car("Ford", "Etzel");
    Car carrera = new Car("Porsche", "Carrera");
    Car grandprix = new Car("Pontiac", "Grand Prix");
    Car pilot = new Car("Honda", "Pilot");
    
    System.out.println("Adding 4 elements to cars Vector()...");
    cars.add(prius);
    cars.add(pinto);
    cars.add(taurus);
    cars.add(maxima);
    
    System.out.println("Check #1 cars size: " + cars.size()); 
    System.out.println("Check #1 cars capacity: " + cars.capacity());
    
    System.out.println("Adding 2 more elements to cars Vector()...");
    cars.add(towncar);
    cars.add(camaro);

    System.out.println("Check #2 cars size: " + cars.size()); 
    System.out.println("Check #2 cars capacity: " + cars.capacity());

    System.out.println("Adding 4 more elements to cars Vector()...");
    cars.add(etzel);
    cars.add(carrera);
    cars.add(grandprix);
    cars.add(pilot);

    System.out.println("Check #3 cars size: " + cars.size()); 
    System.out.println("Check #3 cars capacity: " + cars.capacity());

    System.out.println("Displaying the full list of cars...");
    for (Car c : cars) {
      System.out.format("%s (%s)", c.getName(), c.getManufacturer());
    }
  }
}

Full Program Listing (Car.java)

package com.avaldes.tutorials;

public class Car {
  private String Manufacturer;
  private String name;
  
  public Car(String manufacturer, String name) {
    setManufacturer(manufacturer);
    setName(name);
  }

  public String getManufacturer() {
    return Manufacturer;
  }

  public void setManufacturer(String manufacturer) {
    Manufacturer = manufacturer;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

Output

Initial cars size: 0
Initial cars capacity: 5

Adding 4 elements to cars Vector()...
Check #1 cars size: 4
Check #1 cars capacity: 5

Adding 2 more elements to cars Vector()...
Check #2 cars size: 6
Check #2 cars capacity: 9

Adding 4 more elements to cars Vector()...
Check #3 cars size: 10
Check #3 cars capacity: 13

Displaying the full list of cars...
Prius (Toyota)
Pinto (Ford)
Taurus (Ford)
Maxima (Nissan)
Town Car (Lincoln)
Carmaro (Chevrolet)
Etzel (Ford)
Carrera (Porsche)
Grand Prix (Pontiac)
Pilot (Honda)

Other Related Posts

Map Examples

List Examples

Set Examples

Please Share Us on Social Media

Facebooktwitterredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *