
- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
java.util.Vector.ensureCapacity() Method
Description
The ensureCapacity(int minCapacity) method is used to increases the capacity of this vector, if necessary.This is to ensure that the vector can hold at least the number of components specified by the minimum capacity argument.If the current capacity of this vector is less than minCapacity, then its capacity is increased by replacing its internal data array, kept in the field elementData, with a larger one. The size of the new data array will be the old size plus capacityIncrement.If the value of capacityIncrement is less than or equal to zero then the new capacity will be twice the old capacity.But if this new size is still smaller than minCapacity, then the new capacity will be minCapacity.
Declaration
Following is the declaration for java.util.Vector.ensureCapacity() method
public void ensureCapacity(int minCapacity)
Parameters
minCapacity − This is the desired minimum capacity.
Return Value
It returns void.
Exception
NA
Example
The following example shows the usage of java.util.Vector.ensureCapacity() method.
package com.tutorialspoint; import java.util.Vector; public class VectorDemo { public static void main(String args[]) { // create a vector of initial capacity 5 Vector vec = new Vector(5); for (int i = 0; i < 10; i++) { vec.add(0,i); } System.out.println("Content of the vector: "+vec); System.out.println("Size of the vector: "+vec.size()); // ensure the capacity of the vector and add elements vec.ensureCapacity(40); for (int i = 0; i < 10; i++) { vec.add(0,i); } System.out.println("Content of the vector after increasing the size: "+vec); System.out.println("Size of the vector after increase: "+vec.size()); } }
Let us compile and run the above program, this will produce the following result.
Content of the vector: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] Size of the vector: 10 Content of the vector after increasing the size: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] Size of the vector after increase: 20