This example shows how to create a new Vector object in Java. This example also shows how to create a new object of Vector using various constructors.
How to create a new object of Vector in Java?
The Java Vector class provides several constructors using which we can create new objects of it.
1. How to create a new and empty Vector?
The default no-argument Vector constructor creates a new and empty vector object.
1 |
public Vector() |
The vector object created using the default constructor has the initial capacity of 10 and a capacity increment of 0.
1 |
Vector<Integer> vector = new Vector<Integer>(); |
The above statement will create a new vector object of type Integer having initial capacity of 10 elements.
2. How to create with custom initial capacity and capacity increment?
If you want to specify the initial capacity of the vector object at the creation time, you can use the below given overloaded constructor.
1 |
public Vector(int initialCapacity) |
It creates a new object with the specified initial capacity and the default capacity increment i.e. 0.
1 |
Vector<Integer> vector = new Vector<Integer>( 30 ); |
The above statement will create a new vector whose elements will be of type Integer and having the initial capacity of 30 and capacity increment of 0.
If you want to specify both, the initial capacity and capacity increment, you can use below given overloaded constructor.
1 |
public Vector(int initialCapacity, int capacityIncrement) |
It will create a new object with the specified initial capacity and capacity increment.
3. How to create a vector from other list objects?
The Vector class provides an overloaded constructor that accepts a Collection object as an argument.
1 |
public Vector(Collection<? extends E> collection) |
It will create a new Vector object having all the elements of the specified collection object. The order of the elements will be the same that is returned by the collection object’s iterator. The capacity of this vector object will be sufficient to hold all the elements of the collection object.
The below given example shows how to create a new Vector object from an ArrayList object using this constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.javacodeexamples.collections.vector; import java.util.List; import java.util.ArrayList; import java.util.Vector; public class JavaCreateVectorExample { public static void main(String[] args) { List<Integer> listNumbers = new ArrayList<Integer>(); listNumbers.add(10); listNumbers.add(20); listNumbers.add(30); /* * To create a Vector object from the ArrayList, use * the constructor. */ //this vector will have same elements as the ArrayList Vector<Integer> vector = new Vector<Integer>( listNumbers ); System.out.println("ArrayList contains: " + listNumbers); System.out.println("Vector contains: " + vector); } } |
Output
1 2 |
ArrayList contains: [10, 20, 30] Vector contains: [10, 20, 30] |
This example is a part of the Vector in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Vector Documentation