• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Collection Framework Vector


Vector :
Vector is similar to ArrayList but whose object is synchronized that means only one thread can use that resource at a time. Vector clas also supports same methods of ArrayList class to manage the data in the memory.
Vector<Integer> al=new Vector<Integer>();
Initially memory is allocated for elements later that can be growable to any number of elements
Vector class contains various methods to perform various dynamic operations on the elements.
Vector Methods
1.Vector Add
2.Vector AddIndex
3.Vector AddAll
4.Vector Clear
5.Vector ListClone
7.Vector Contains
8.Vector Get
9.Vector IndexOf
10.Vector IsEmpty
11.Vector Remove
12.Vector Set
13.Vector Size
14.Vector ToArray


Key Points

Image






Vector Add Method:
Which can be used to either add or insert the element.
Syntax:
al.add(element);
Which can be used to add the an element at the endof Vector.


import java.util.*;
class VectorAdd
{
void list()
{
Vector<Integer> al=new Vector<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
}
}
class Main
{
public static void main(String aargs[])
{
VectorAdd d=new VectorAdd();
d.list();
}
}


Output:

Vector AddIndex Method:
Which can be used to insert a new element at the given index value.
Syntax:
al.add(index,element);


import java.util.*;
class VectorAddIndex
{
void list()
{
Vector<Integer> al=new Vector<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
al.add(2,2000);//it will insert 2 index element
System.out.println("The List Items Are "+al);
}
}
class Main
{
public static void main(String aargs[])
{
VectorAddIndex d=new VectorAddIndex();
d.list();
}
}


Output:

Vector AddAll Method:
Which can be used to add all elements of one Vector to another Vector.
Syntax: al2.addAll(al1);
Elements of al1 objects will be added to al2 object.


import java.util.*;
class VectorAddAll
{
void list()
{
Vector<Integer> al=new Vector<Integer>();
al1.add(20);
al1.add(30);
al1.add(40);
al1.add(10);
al1.add(70);
System.out.println("The List Items Are "+al1);
Vector<Integer> al2=new Vector<Integer>();
al2.addAll(al1);
System.out.println("The List Items Are "+al2);
}
}
class Main
{
public static void main(String aargs[])
{
VectorAddAll d=new VectorAddAll();
d.list();
}
}


Output:

Vector Clear Method :
Which can be used to remove all the elements from an Vector. Syntax:
al.clear();


import java.util.*;
class VectorClear
{
void list()
{
Vector<Integer> al=new Vector<Integer>();
al1.add(20);
al1.add(30);
al1.add(40);
al1.add(10);
al1.add(70);
System.out.println("The List Items Are "+al1);
al1.clear(); System.out.println("The List Items Are "+al1);
}
}
class Main
{
public static void main(String aargs[])
{
VectorClear d=new VectorClear();
d.list();
}
}


Output:

Vector ListClone Method
Which can be used to get the get cloning object or duplicate object from the existing array list object.
Syntax: Vector<Integer> al2;
al2=(Vector)al.clone();
Duplicate object is created for 'al1' for whose reference is assigned to al2.

Difference between clone and addAll methods? Close method will create duplicate memory from the existing memory. Where as addAll elements one existing object to another existing object.


import java.util.*;
class VectorClone
{
void list()
{
Vector<Integer> al=new Vector<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
Vector<Integer> al2;
al2=(Vector)al.clone();
System.out.println("After Cloning The List Items Are "+al2);
}
}
class Main
{
public static void main(String aargs[])
{
VectorClone d=new VectorClone();
d.list();
}
}


Output:

Vector Contains Method :
It is a boolean method,it return true if the given element existing in the list otherwise returns false.
al.contains(element);


import java.util.*;
class VectorContains
{
void list()
{
Vector<Integer> al=new Vector<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
System.out.println(al.contains(20));
}
}
class Main
{
public static void main(String args[])
{
VectorContains d=new VectorContains();
d.list();
}
}


Output:

Vector Get Method
It is used to find element based on index value.
al.get(indexvalue);


import java.util.*;
class VectorGet
{
void list()
{
Vector<Integer> al=new Vector<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
System.out.println(al.get(1));
}
}
class Main
{
public static void main(String aargs[])
{
VectorGet d=new VectorGet();
d.list();
}
}


Output:

Vector IndexOf Method
Which can be used to find the index value based on the element or object.
al.indexOf(element);


import java.util.*;
class VectorIndexOf
{
void list()
{
Vector<Integer> al=new Vector<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
System.out.println(al.indexOf(10));
}
}
class Main
{
public static void main(String aargs[])
{
VectorIndexOf d=new VectorIndexOf();
d.list();
}
}


Output:

Vector IsEmpty Method
Which can be used to it return true the list is empty otherwise it returns false.
al.isEmpty();


import java.util.*;
class VectorIsEmpty
{
void list()
{
AVector<Integer> al=new Vector<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
System.out.println(al.isEmpty());
}
}
class Main
{
public static void main(String args[])
{
VectorIsEmpty d=new VectorIsEmpty();
d.list();
}
}


Output:

Vector Remove Method
Which can be used to remove a specific element based on index value.
al.remove(indexvalue);
It can be removed not only based on index value but also it is possible throw element or value.


import java.util.*;
class VectorRemove
{
void list()
{
Vector<Integer> al=new Vector<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
al.remove(2);
System.out.println("After Remove The List Items Are "+al);
}
}
class Main
{
public static void main(String args[])
{
VectorRemove d=new VectorRemove();
d.list();
}
}


Output:

Vector Set Method
Which can be used to replace a new element with old element based on the index value.
al.set(indexvalue,element);


import java.util.*;
class VectorSet
{
void list()
{
Vector<Integer> al=new Vector<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
al.set(1,1000);
System.out.println("After Set The List Items Are "+al);
}
}
class Main
{
public static void main(String args[])
{
VectorSet d=new VectorSet();
d.list();
}
}


Output:

Vector Size Method:
Which can be used to find the size of array list(number of elements of an Vector)
al.size();


import java.util.*;
class VectorSize
{
void list()
{
Vector<Integer> al=new Vector<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
System.out.println(al.size());
}
}
class Main
{
public static void main(String args[])
{
VectorSize d=new VectorSize();
d.list();
}
}


Output:

Vector ToArray Method:
It returns any array containing all the elements of Vector so that can be use the element of array but no manifulation can be done.
al.toArray();


mport java.util.*;
class VectorToArray
{
void list()
{
Vector<Integer> al=new Vector<Integer>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);
System.out.println("The List Items Are "+al);
System.out.println(al.toArray());
}
}
class Main
{
public static void main(String aargs[])
{
VectorToArray d=new VectorToArray();
d.list();
}
}
Note: For all the list intrface implemented classes 50% of current location on increased once default memory locations are filled with the element. Example: Initially for an array list 10 momory locations are created. After storing 10th element 5 memory location will be added(10/2). after storing 15th element 7 memory locations will be added(15/2 or (10+5)/2).
An Vector class object is not synchronized that means multiple threads can work simultanusly on the same resource.


Output: