As vector is grow able in nature in Java, it increases its size dynamically depending upon the current number of elements present in the vector. However we can control this dynamic growth of vector.

Note: While this tutorial uses Vector which should be used in legacy apps. where Vector is being already used or the app. is running on an older version of JDK. The example and settings can also be applied to ArrayList.

The initial capacity defines the number of initial elements that can be accommodated in a vector object without allocating more memory dynamically. The dynamic growth operation consumes crucial CPU cycles. By visualizing the maximum number of elements which are going to be present in the vector we can avoid the run-time grew in the size of vector and hence save those CPU cycles.

However if the initial capacity value is set to very high number and those many elements are not eventually stored in the vector, it results in the wastage of memory which could have been used for some other purpose.

The following program shows how to use the initial capacity with vector class:

package com.example;

import java.util.Vector;

public class Test{
 
	public static void main(String[] args) {
		Vector v = new Vector(100);
		for(int i=0;i<60;i++) {
			v.add("obj"+i);
		}
		System.out.println(v.size());
		System.out.println(v.capacity());
	}
}

Output:

60
100

The output shows that the capacity of vector is 100 but only 60 elements have been stored.

  • Another related attribute of Vector is the capacityIncrement which decides how much the Vector will grow in length when its current capacity is full.
  • The ArrayList class also has the concept of initial capacity but doesn’t provide an mechanism to get the capacity value once it has been set by using the constructor.
  • By default both ArrayList and Vector have an initial capacity of 10.
Setting initialcapacity of Vector, ArrayList in Java admin Core Java
As vector is grow able in nature in Java, it increases its size dynamically depending upon the current number of elements present in the vector. However we can control this dynamic growth of vector. Note: While this tutorial uses Vector which should be used in legacy apps. where Vector is...
As vector is grow able in nature in Java, it increases its size dynamically depending upon the current number of elements present in the vector. However we can control this dynamic growth of vector. <strong>Note:</strong> While this tutorial uses Vector which should be used in legacy apps. where Vector is being already used or the app. is running on an older <a href="http://www.javaexperience.com/java-difference-between-jvm-jre-jdk-jit/" title="Difference between JVM JRE JDK JIT">version of JDK</a>. The example and settings can also be applied to <a href="http://www.javaexperience.com/differences-between-arraylist-and-vector/" title="Differences between ArrayList and Vector">ArrayList</a>. The initial capacity defines the number of initial elements that can be accommodated in a vector object without <a href="http://www.javaexperience.com/jvm-memory-areas-and-categories/" title="JVM Memory areas">allocating more memory dynamically</a>. <strong>The dynamic growth operation consumes crucial CPU cycles.</strong> By visualizing the maximum number of elements which are going to be present in the vector we can avoid the run-time grew in the size of vector and hence save those CPU cycles. However if the initial capacity value is set to very high number and those many elements are not eventually stored in the vector, it results in the wastage of memory which could have been used for some other purpose. The following program shows how to use the initial capacity with vector class: 1 <strong>Output:</strong> <blockquote> 60 100 </blockquote> The output shows that the capacity of vector is 100 but only 60 elements have been stored. <ul> <li> Another related attribute of Vector is the capacityIncrement which decides how much the Vector will grow in length when its current capacity is full. </li> <li> The ArrayList class also has the concept of initial capacity but doesn't provide an mechanism to get the capacity value once it has been set by using the constructor. </li> <li> By default both ArrayList and Vector have an initial capacity of 10. </li> </ul>
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.