Vector in Java
Vector:- Vector implements a dynamic array that means it will grow or shrink as required.
It contains components that can be accessed using the integer index. Vector can hold only objects, not primitive types.
ex:- int,
Creating a Vector:-
You must import a package called java.util.vector or java.util.* .
Vector v1 = new Vector();
-It creates a default size of 20.
Vector v = new Vector(50);
Vector v = newVector(2, 3);
The Initial size is 2 and capacity increment is 3 it means up to insertion of 3rd element the size would be 5 (2+3) and the 6th insertion (5+3)=8.
What is Vector? How it differs from an array?
We have already gone through the Array. Array is a set of similar data types which has static memory allocation. Here, Vector is a dynamic array that can hold objects of any type.
In Java, Vector is one of the collection framework classes defined in java.util package.
That means in Vector also there are elements and indexing which starts from zero.
The following is the signature of this class.
public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable.
Constructor:-
The class Vector defines the following overloaded constructors.
-vector()
-Vector(int size)
-Vector(int size, int increment)
We can see that Vector's object implements dynamics array. Vector class comes under java.util package.
The first constructor creates a Vector of default size 10. The second constructor creates a vector of a specified size/index.
The third constructor creates a vector of specified size and increment is specified by inner.
Here is an example of each constructor used with a vector object.
-Vector v1 = new Vector();
-Vector v2 = new Vector(15);
-Vector v3 = new Vector(12, 3);
Note that, we can't directly store values in a vector. We have to use Wrapper classes and their constructors which are stored as an object in a vector.
List of method common used methods of class Vector:-
Here is a list of some Vector methods.
1)-void addElement(Object obj):-
Add specified object as Vector element into invoking vector object.
2)-int capacity():-
Return the capacity of invoking a Vector object.
3)-boolean contains(Object obj):-
Returns true if the specified object is present in invoking Vector object otherwise return false.
4)-Object elementAt(int index):-
Returns the object present at specified at index in invoking Vector Object.
5)-Object firstElement():-
Returns the first element of the invoking Vector Object.
6)-boolean isEmpty():-
Return true if invoking the Vector object is empty, otherwise false.
7)-void lastElement():-
Returns the last element of the invoking Vector Object.
8)-void removeElement(Object obj):-
Remove the specified object from the invoking Vector object.
9)-void removeAllElement():-
Empties the Vector and size becomes zero.
10)-void removeElementAt(int index):-
Removes element present at the specified index in invoking vector object
11)- void setSize(int size):-
Updates the new size of the invoking Vector object.
12)-int size():-
Returns the numbers of elements present in the invoking Vector object.
Example of Vector:-
import java.util.*;
class test
{
public static void main(String args[])
{
Vector<string> v1= new Vector<string>
v1.add("Red");
v1.add("Blue");
v1.add("Yellow");
System.out.println(v1);
v1.add(2, "Green");
System.out.println(v1);
System.out.println("element of the 3rd position"+ v1.get(3));
}
}
Ex;-2:-
Insertion of Elements in Vector.
import java.util.*;
class insertOperation
{
public static void main(String args[])
{
Vector v1 = new Vector(5, 1);
//declaring wrapper class objects to add into vector
Integer i1 = new Integer(11, 5);
Double d1 = new Double(45.36);
Long l1 = new Long(546570);
String s1 = new String("Vishmyblog");
//Adding wrapper class objects into vector
v1.addElement(i1);
v1.addElement(d1);
v1.addElement(l1);
v1.addElement(s1);
int x = v1.size();
System.out.println("\n Displaying Vector elements\n");
for(int i = 0; i<x; i++)
{
System.out.println(v.elementAt(i)+ "\n");
}
}
}
Program to demonstrate methods of Vector.
import java.util.*;
class VectorMethods
{
public static void main(String args[])
{
Vector v1 = new Vector(3);
int i;
System.out.println("\nInitial Size of Vector is:"+ v1.size());
System.out.println("Initial capacity of Vector is:"+ v1.capacity());
System.out.println("Initially, is Vector empty :"+ v1.isEmpty());
Integer i1 = new Integer(55);
v1.addElement(new Integer(15));
v1.addElement(new Float(23.55f));
v1.addElement(new Long(12345l));
v1.addElement("vishmyblog");
v1.addElement(i1);
System.out.println("After adding one element Size of the vector is:"+ v1.size());
System.out.println("After adding elements Capacity of the vector is :"+ v1.capacity());
System.out.println("After adding elements, is vector empty? :"+ v1.isEmpty());
System.out.println("is Vector contains i1? :"+ v1.contains());
System.out.println("\nVector Elements are\n");
for(int i = 0; i<x; i++)
{
System.out.println(v1.elementAt());
}
v1.removeElement(i1);
System.out.println("After removing one element Size of the vector is:"+ v1.size());
System.out.println("After removing one element Capacity of the vector is :"+ v1.capacity());
System.out.println("After removing one element, is vector empty? :"+ v1.isEmpty());
System.out.println("is Vector contains i1? :"+ v1.contains());
v1.removeElementAt(2);
System.out.println("After removing one element Size of the vector is:"+ v1.size());
System.out.println("After removing one element Capacity of the vector is :"+ v1.capacity());
System.out.println("After removing one element, is vector empty? :"+ v1.isEmpty());
v1.removeAllElement();
System.out.println("After removing one element Size of the vector is:"+ v1.size());
System.out.println("After removing one element Capacity of the vector is :"+ v1.capacity());
System.out.println("After removing one element, is vector empty? :"+ v1.isEmpty());
}
}
0 Comments