Java Vector

Vectors are used instead of arrays because unlike array they can adjust according to the component added or removed from them.

Java Vector

Vectors are used instead of arrays because unlike array they can adjust according to the component added or removed from them.

Java Vector

Vectors in Java are used because they can expand whenever a new data is added to them. They also shrink when data is removed from them. The Vector class implements dynamic array of objects.

Vectors can only hold objects. Components are added to Vector during runtime.

Items can be removed even after the Vector has been created.

Vectors are array list with extended properties which follow the dynamic and automatic addition of data at run-time. The Component in vector class can be accessed by integer index.

Capacity of vector is usually larger as components are added in it during run time.

Size of vectors increases in chunks.

Vector instances are like linked-lists, whenever they reach maximum limit, resources are allocated dynamically and its size is increased further.

To create a Vector import java.util.Vector or java.util.*

How to declare Vector

Following Syntax is used to declare an empty vector so that its standard storage capacity is zero

public Vector()

Following Syntax constructs an empty vector specified by initial capacity:

vector(int initial capacity)

Following Syntax creates an empty vector with the specified initial capacity and capacity increment:

vector(int initial capacity,int capacity increment)

Method used in Vector

Following method inserts the specified element at the specified position in the Vector:

void add(int index, Object element)

Following method appends the specified element to the end of the Vector:

boolean add(object o)

Following method return the current capacity of the vector:

int capacity( )

Following method removes all of the elements from the Vector:

void clear( )

Following method sets the new size of Vector

void setsize(int new size )

Following method returns the number of Component in Vector:

int size( )

Following method trims the Capacity of Vector to vector's current size:

Vector trim to size( )

0

Following method returns the occurrence of last index of the specified object in the vector:

public int lastindexof(obj abc)

Following is the Example of Java Vector:

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

Maximum Element of java Vector is :900