Java Vector

Vector are array list with extended properties which follow the dynamic and automatic addition of data at run-time.

Java Vector


Introduction to Vector

Vector are array list with extended properties which follow the dynamic and automatic addition of data at run-time. Unlike array, Vector can grow and  shrink as needed to accommodate its size adding and removing items even after the Vector has been created. The Vector class implements dynamic array of objects. The Component in vector class can be accessed by integer index. Each vector tries to maintain its  storage size by capacity and a capacity Increment. The capacity of vector is always least as of  its size and usually larger because  components are added to the vector at run-time. The vector's storage get increase in chunks as the size of capacity Increment. An application increase the size of a vector before inserting a large number of components that is used to  reduce the amount of increment in reallocation. Vectors can be easily used.. It implement the Enumeration interface which makes  the contents of a Vector  traversed

Vector instances are like linked-lists that can grow dynamically. Once it reaches the maximum limit, resources are allocated dynamically and increases its size further.

How to declare Vector


The given below Syntax is used to declare an empty vector so that the size of  internal data array has 10 and its standard storage capacity has zero.

public Vector()

The given Syntax is used to construct an empty vector specified by initial capacity.

vector(int initial capacity)

The below Syntax Creates an empty vector with the specified initial capacity and capacity increment.

vector(int initial capacity,int capacity increment)

Method used in Vector

void add(int index, Object element)-  Inserts the specified element at the specified position in this Vector

boolean add(object o)- Appends the  specified element to the end of this Vector.

int  capacity( ) -  Return the current capacity of this vector 

void clear( )-  Removes all of the elements from this Vector.

void setsize(int new size )-Set the new size of Vector.

int size( )-Return the number of Component in Vector.

Vector trim to size( )- Trim the Capacity of Vector to vector's current size.

public int lastindexof(obj abc)-Returns the occurrence of last index of of the specified object in this vector.

Understand Vector by Example-

This java example shows us to find a maximum element of Java Vector using max method of Collections class. We Import required package, then declare a Class name maximum vector element .Inside the main method create an object of vector. The object of vector class calls add method to add the component of vector. In order to find the maximum element of vector component use, static object max(collection c) method of collection class which we already imported the Collection package .This method returns the maximum element of Java Vector.


import java.util.Vector;
import java.util.Collections;

public class maximum vector element


public static void main(String[] args)

//create a Vector object
Vector v = new Vector();
//Add elements to Vector
v.add(new int("500"));
(new int("745"));
(new int("842"));
(new int("900"));
(new int("567"));

Object obm = Collections.max(v);
("Maximum Element of Java Vector is : " + obm);


Output of above code-

Maximum Element of java Vector is :900

Share on Google+Share on Google+

Java Vector

Posted on: October 22, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Java Vector   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
August 22, 2011

simple and best example of vector
December 3, 2011
hi solve this

i write this code on jave import; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.lang.SecurityException; /** * Basic symmetric encryption example with padding and CBC using DES */ public class NewClass1 { public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "harivardhanan".getBytes(); byte[] keyBytes = new byte[] { 0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef }; byte[] ivBytes = new byte[] { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 }; SecretKeySpec key = new SecretKeySpec(keyBytes, "DES"); IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS7Padding", "BC"); System.out.println("input : " + new String(input)); // encryption pass cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; System.out.println("input.length"+input.length); int ctLength = cipher.update(input, 0, input.length, cipherText, 0); ctLength += cipher.doFinal(cipherText, ctLength); System.out.println("cipher: " + new String(cipherText) + " bytes: " + ctLength); // decryption pass cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); byte[] plainText = new byte[cipher.getOutputSize(ctLength)]; int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0); ptLength += cipher.doFinal(plainText, ptLength); System.out.println("plain : " + new String(plainText) + " bytes: " + ptLength); if(input.equals(plainText)) System.out.println("equal"); } } then this type of exception will appear Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any> at newpackage.NewClass1.main( Java Result: 1
December 3, 2011
hi solve this

how can i solve this problem: Exception in thread"AWT-EventQueue-0" java.lang.UnsupportedClassVersionError:s impleProtector:Unsupported major-minor Version 51.0
July 8, 2012

thanks for helping.....god & easy understanding for vector in java..nice....