Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to Initialize a Vector with a Specific Initial Capacity in Java?
Next article icon

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

Last Updated : 12 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

Java
// Java program to initialize a  // Vector with a specific initial capacity import java.io.*; import java.util.Vector;  // Driver Class public class GFG {     // Main Function     public static void main(String[] args) {         // Initializing a Vector with         // the specific initial capacity         int Capacity = 10;         Vector<String> stringVector = new Vector<>(Capacity);                  // Adding elements to Vector         stringVector.add("Java");         stringVector.add("is");         stringVector.add("powerful.");                  // Displaying the elements of the Vector         System.out.println("The Vector elements: " + stringVector);     } } 

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.

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

M

maha123
Improve
Article Tags :
  • Java
  • Java Programs
  • Java-Collections
  • Java-Vector
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

    How to Replace an Element at a Specific Index of the Vector in Java?
    The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in the java.util package and implements the List interface, so we can use all the methods of List interface here. Examples Input : Vector= ["
    2 min read
    How to Fill (initialize at once) an Array in Java?
    An array is a group of like-typed variables that are referred to by a common name. In this, article we will learn about Filling array in Java while Initialization.Example:Java// Java program to fill the element in an array import java.util.*; public class Geeks { public static void main(String args[
    3 min read
    How to Create a HashSet With a Predefined Capacity in Java?
    In Java, we can create HashSet with a predefined capacity by creating a constructor and passing the capacity as a parameter to it. We have to initialize the capacity using a variable otherwise you can pass a direct value to it. Syntax:HashSet <datatype>myset=new HashSet<>(capacity);Here,
    1 min read
    How to Create a HashSet with a Custom Initial Load Factor in Java?
    Java HashSet is a simple data structure provided by the Java Collection Framework that provides efficient storage and enables the storage of unique objects. One of the parameters that affect its performance is the load factor, which determines when the underlying hash table should be updated to acco
    2 min read
    How to Implement a Custom Vector with Additional Functionality in Java?
    In Java, the Vector class is a part of the Java Collections Framework providing the Dynamic Arrays that can be resized. However, there might be scenarios where you need a Custom Vector with additional functionality tailored to your specific requirements. PrerequisiteTo follow along, you should have
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences