Vector in Java

Definition

  • Vector is a separate class in java that implements a dynamic array(not as simple static array)that means it can grow or shrink its size as required in the program dynamically.

Characteristics

  • The Vector class implements a growable array of objects.
  • Like an simple/general/normal/generic/static array, it contains components that can be accessed using an integer index.
  • It is a part of the collection framework and it extends AbstractList and implements List interfaces i.e. they are implementations of the List interface.
  • It is suggested that use normal array if the size is fixed or use vector if the size may change(synchronized) or ArrayList(Non-synchronized).
  • It maintains the insertion order of elements as simple array.

Advantages

  • They provide dynamic array concept i.e. array itself expands or shrinks their size automatically as per requirement of the program.
  • They support generic array concept i.e. any array cell can be accessed/referenced using index value of the cell.
  • They have many useful predefined methods to do jobs easily.

Disadvantages

  • When using Vector /ArrayList, it is suggested that always try to initialize it to the largest capacity that our program will need, since expanding the array is costly. This is because in array expansion, allocation of a larger array is done first and then copy contents of old array to the new one.
  • It is better to use in sensitive and specific applications.

ArrayList in Java

Definition

  • ArrayList is also a separate class in java that implements a dynamic array i.e. it can grow or shrink its size as required in the program dynamically.

Characteristics

  • Similar as Vector concept.
  • ArrayList (non-synchronized) can be synchronized as vector using the java collections framework utility class and then ArrayList itself can be used in place of Vector.

Advantages

  • Similar as Vector concept.

Disadvantages

  • Similar as Vector concept.
Click this link to see the difference between Vector and ArrayList

Examples

Example : A java vector program to store and display values in a vector class.

import java.util.Vector;
class Example 
{ 
    public static void main(String[] arg) 
    {        
        Vector vc = new Vector();  
        vc.add(1);     // add() method is used to store the data in vector
        vc.add(02); 
	vc.add('A'); 
        vc.add("Codershelpline"); 
        vc.add("IGNOU"); 
        vc.add(3.14); 
  
        System.out.println("The different output of Vector class is " + vc); 
    } 
}
Output:
F:\java>javac vector1.java
Note: vector1.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

F:\java>java Example
    The different output of Vector class is [1, 2, A, Codershelpline, IGNOU, 3.14]
 
NB : It itself assign the index with given data.

--------  OR  --------
import java.util.Vector; 

class Example 
{ 
    public static void main(String[] arg) 
    {         
        Vector vc = new Vector();  
        vc.add(0,1); 
        vc.add(1,02); 
	vc.add(2,'A'); 
        vc.add(3,"Codershelpline"); 
        vc.add(4,"IGNOU"); 
        vc.add(5,3.14); 
  
        System.out.println("The different output of Vector class is " + vc); 
    } 
}
Output:
     The different output of Vector class is [1, 2, A, Codershelpline, IGNOU, 3.14] 
NB : Here a systematic index is given with data by the programmer. 

Example : A java vector program to store, display and clean the data from a vector class.

import java.util.Vector; 

class Example 
{ 
    public static void main(String[] arg) 
    {        
        Vector vc = new Vector();  
        vc.add(0,1); 
        vc.add(1,02); 
	vc.add(2,'A'); 
        vc.add(3,"Codershelpline"); 
        vc.add(4,"IGNOU"); 
        vc.add(5,3.14);  
        System.out.println("The different output of Vector class is " + vc);

	vc.clear();        
        System.out.println("After clearing the Vector we have : " + vc); 		
    } 
}
Output:
    The different output of Vector class is [1, 2, A, Codershelpline, IGNOU, 3.14]
    After clearing the Vector we have : []

Example : A java vector program to store and display unique data from a vector class.

import java.util.Vector;
class Example 
{ 
    public static void main(String[] arg) 
    {        
        Vector vc = new Vector();  
        vc.add(1); 
        vc.add(02); 
	vc.add('A'); 
        vc.add("Codershelpline"); 
        vc.add("IGNOU"); 
        vc.add(3.14);  
        System.out.println("The output of element at given index is " + vc.get(5));			
    } 
}
Output:
The output of element at given index is 3.14

Example : A java vector program to store and display values from a vector class using Iterator Interface.

import java.util.Vector; 
import java.util.Iterator;
class Example 
{ 
    public static void main(String[] arg) 
    {        
        //Declaration of vector's instance to store only string data.      
        Vector<String> vc = new Vector<String>();  
		
        vc.add("1");             //method of Collection framework
        vc.addElement("02");     //method of Vector class
	vc.addElement("A"); 
        vc.addElement("Codershelpline"); 
        vc.addElement("IGNOU"); 
        vc.addElement("3.14");		
		
	System.out.println("The output at given index is " + vc.get(3));
		
	System.out.println("The output of all elements are = " + vc);
		
	System.out.println(); 
		  
	//Traversing elements using Iterator Interface  
	Iterator ir = vc.iterator();  // 				
	   while(ir.hasNext())
	     {
	        System.out.println(ir.next());
	     }
      }
}

Example : A java vector program to store and display values of a vector class using Enumeration Interface.

//import java.util.*;
import java.util.Vector; 
import java.util.Enumeration;
class Example 
{ 
    public static void main(String[] arg) 
    {        
          //Declaration of vector's instance to store only stirng data.        
        Vector<String> vc = new Vector<String>();  
		
        vc.add("1");             //method of Collection framework
        vc.addElement("02");     //method of Vector class
	vc.addElement("A"); 
        vc.addElement("Codershelpline"); 
        vc.addElement("IGNOU"); 
        vc.addElement("3.14");		
		
	System.out.println("The output at given index is " + vc.get(5));
		
	System.out.println("The output of all elements are = " + vc);
		
	System.out.println();
		  
	//Traversing elements using Enumeration Interface  
	Enumeration en=vc.elements(); 			
	   while(en.hasMoreElements())
	     {  
	      System.out.println(en.nextElement());
             } 
     }
}
Output:
The output at given index is 3.14
The output of all elements are = [1, 02, A, Codershelpline, IGNOU, 3.14]

1
02
A
Codershelpline
IGNOU
3.14

--------  OR  --------
//import java.util.*;
import java.util.Vector; 
import java.util.Enumeration;
class Example 
{ 
    public static void main(String[] arg) 
    {        
         //Declaration of vector's instance to store only integer data.     
        Vector<Integer> vc = new Vector<Integer>();  
		
        vc.add(1);             //method of Collection framework
        vc.addElement(02);     //method of Vector class				
		
	System.out.println("The output at given index is " + vc.get(1));
		
	System.out.println("The output of all elements are = " + vc);
		
	System.out.println(); 
		  
	//Traversing elements using Enumeration Interface  
	Enumeration en=vc.elements();			
	while(en.hasMoreElements())
	  {  
	     System.out.println(en.nextElement());        				
	  } 
     }
}
Output:
The output at given index is 2
The output of all elements are = [1, 2]

1
2

Example : A java ArrayList program to store and display the integer values from a ArrayList class.

import java.util.ArrayList;
class Example 
{ 
    public static void main(String[] arg) 
    {        
		ArrayList<Integer> Ir = new ArrayList<Integer>();
		Ir.add(10);
		Ir.add(2032);
		Ir.add(0012);
		Ir.add(10000);
		for (int i : Ir) 
		{
		   System.out.println(i);
		}		
     }
}	
Output :
10
2032
10
10000

Example : A java ArrayList program to store and display the sorted integer values from a ArrayList class.

import java.util.ArrayList; 
import java.util.Collections; 
class Example 
{ 
    public static void main(String[] arg) 
    {        
		ArrayList<Integer> Ir = new ArrayList<Integer>();
		Ir.add(100);
		Ir.add(2032);
		Ir.add(12);
		Ir.add(10);
		Collections.sort(Ir);
		for (int i : Ir) 
		{
		   System.out.println(i);
		}		
     }
}
Output :
10
12
100
2032	

Example : A java ArrayList program to store and display string values from a ArrayList class.

import java.util.ArrayList;
class Example 
{ 
    public static void main(String[] arg) 
    {        
		ArrayList<String> al = new ArrayList<String>();
		al.add("BCA");
		al.add("BBA");
		al.add("MBA");
		al.add("MCA");
		System.out.println(al);
    }
}	
Output :
[BCA, BBA, MBA, MCA]

Example : A java ArrayList program to store and display unique string data from a ArrayList class.

import java.util.ArrayList;
class Example 
{ 
    public static void main(String[] arg) 
    {        
		ArrayList<String> al = new ArrayList<String>();
		al.add("BCA");
		al.add("BBA");
		al.add("MBA");
		al.add("MCA");
		System.out.println(al.get(2));
    }
}
Output :
MBA	

Example : A java ArrayList program to store and display the edited values from a ArrayList.

import java.util.ArrayList;
class Example 
{ 
    public static void main(String[] arg) 
    {        
		ArrayList<String> al = new ArrayList<String>();
		al.add("BCA");
		al.add("BBA");
		al.add("MBA");
		al.add("MCA");
		System.out.println(al);
		al.set(1,"M.Tech.");
		System.out.println(al);
     }
}
Output :
[BCA, BBA, MBA, MCA]
[BCA, M.Tech., MBA, MCA]	

Example : A java ArrayList program to store and display the removed values from a ArrayList.

import java.util.ArrayList; 
class Example 
{ 
    public static void main(String[] arg) 
    {        
		ArrayList<String> al = new ArrayList<String>();
		al.add("BCA");
		al.add("BBA");
		al.add("MBA");
		al.add("MCA");
		System.out.println(al);
		al.remove(1);
		System.out.println(al);
    }
}
Output :
[BCA, BBA, MBA, MCA]
[BCA, MBA, MCA]

Example : A java ArrayList program to store and display the removed values from a ArrayList.

import java.util.ArrayList;
class Example 
{ 
    public static void main(String[] arg) 
    {        
		ArrayList<String> al = new ArrayList<String>();
		al.add("BCA");
		al.add("BBA");
		al.add("MBA");
		al.add("MCA");
		System.out.println(al);
		al.clear();
		System.out.println(al);
    }
}	
Output :
[BCA, BBA, MBA, MCA]
[]

Example : A java ArrayList program to store and display the data and size of a ArrayList.

import java.util.ArrayList;
class Example 
{ 
    public static void main(String[] arg) 
    {        
		ArrayList<String> al = new ArrayList<String>();
		al.add("BCA");
		al.add("BBA");
		al.add("MBA");
		al.add("MCA");
		System.out.println(al.size());
		
		System.out.println(al);
     }
}
	
Output :
4
[BCA, BBA, MBA, MCA]

Example : A java ArrayList program to store and display the data using for loop from a ArrayList.

import java.util.ArrayList;
class Example 
{ 
    public static void main(String[] arg) 
    {        
		ArrayList<String> al = new ArrayList<String>();
		al.add("BCA");
		al.add("BBA");
		al.add("MBA");
		al.add("MCA");
		for (int i = 0; i < al.size(); i++) 
		{
		    System.out.println(al.get(i));
		}		
     }
}	
Output :
BCA
BBA
MBA
MCA

Example : A java ArrayList program to store and display the data using for each loop from a ArrayList.

import java.util.ArrayList;
class Example 
{ 
    public static void main(String[] arg) 
    {        
		ArrayList<String> al = new ArrayList<String>();
		al.add("BCA");
		al.add("BBA");
		al.add("MBA");
		al.add("MCA");
		for (String i : al) 
		{
		   System.out.println(i);
		}		
     }
}
Output :
BCA
BBA
MBA
MCA	

Example : A java ArrayList program to store and display the sorted string values from a ArrayList class.

import java.util.ArrayList;
import java.util.Collections;
class Example 
{ 
    public static void main(String[] arg) 
    {        
		ArrayList<String> al = new ArrayList<String>();
		al.add("BCA");
		al.add("BBA");
		al.add("MBA");
		al.add("MCA");
		System.out.println(al);
		
		Collections.sort(al);  
		for (String i : al) 
		{
		   System.out.println(i);
		}
    }
}	
Output :
[BCA, BBA, MBA, MCA]
BBA
BCA
MBA
MCA

 441 total views,  1 views today

Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.