Java Online Compiler

							
							

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 Add Method:
Vector
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 arraylist.


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();
}
}

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();
}
}

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> al1=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();
}
}

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>();
al.add(20);
al.add(30);
al.add(40);
al.add(10);
al.add(70);

System.out.println("The List Items Are "+al);
al.clear();
System.out.println("After Clear The List Items Are "+al);
}
}

class Main
{
public static void main(String aargs[])
{
VectorClear d=new VectorClear();
d.list();
}
}

Vector Clone 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();
}
}

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 aargs[])
{
VectorContains d=newVectorContains();
d.list();
}
}

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();
}
}

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(100));
}
}

class Main
{
public static void main(String aargs[])
{
VectorIndexOf d=new VectorIndexOf();
d.list();
}
}

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()
{
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.isEmpty());
}
}

class Main
{
public static void main(String aargs[])
{
VectorIsEmpty d=new VectorIsEmpty();
d.list();
}
}

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 aargs[])
{
VectorRemove d=new VectorRemove();
d.list();
}
}

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 aargs[])
{
VectorSet d=new VectorSet();
d.list();
}
}

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 VectortSize
{
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 aargs[])
{
VectorSize d=new VectorSize();
d.list();
}
}


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();    

import 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 synchronized that means multiple threads can not work simultanusly on the same resource.