4:15 am - Thursday July 19, 2018

Vector Example Program for ensureCapacity(int minCapacity)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044
/**
@AUTHOR: candid java
@DESCRIPTION: increasing the vector capacity from its initial capacity
 */

import java.util.Vector;

public class Vec {
	public static void main(String args[]) {
		Vector vec = new Vector(5); // create a vector of capacity 5
		for (int i = 0; i < 10; i++) {
			vec.add(0, i); // adding the content to the vector
		}
		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); // increasing the vector size upto 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());
	}
}

OUTPUT:
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

Filed in: Z-A All Java Codes

Comments are closed.