VOOZH about

URL: https://www.geeksforgeeks.org/java/initialize-a-vector-with-a-specific-initial-capacity-in-java/

⇱ How to Initialize a Vector with a Specific Initial Capacity in Java? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Initialize a Vector with a Specific Initial Capacity in Java?

Last Updated : 23 Jul, 2025

In Java, Vector Class allows to creation of dynamic arrays that can grow or shrink as per the need. If we know the approximate size of elements, we will store this in the vector. Then we optimize memory usage, and we can initialize it with the specific initial capacity.

In this article, we will learn how to initialize a Vector with a specific initial capacity in Java.

Example Input-Output:

Input: int initialCapacity = 10;Vector<String> string
Vector = new Vector<>(initialCapacity);

Output: A Vector named stringVector is created with the initial capacity of 10.

Syntax:

Vector<E> vector = new Vector<>(initialCapacity);
  • E: The type of elements in Vector.
  • initialCapacity: The initial capacity of the Vector. It represents the number of elements the Vector can initially store without the resizing.

Program to Initialize a Vector with Initial Capacity in Java

Below is the implementation to Initialize a Vector with Initial Capacity:


Output
The Vector elements: [Java, is, powerful.]

Explanation of the Program:

In the above program,

  • Initialization: The initial capacity is set to 10. A Vector named stringVector is created with this initial capacity.
  • Adding Elements: Three string elements are added to stringVector.
  • Displaying Elements: The elements of stringVector are printed to console.
Comment