Java Vector

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

Java Vector

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

Java Vector

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

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"));
  v.add
(new int("745"));
  v.add
(new int("842"));
  v.add
(new int("900"));
  v.add
(new int("567"));
 
 

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

 

Output of above code-

Maximum Element of java Vector is :900