Introduction
- In Java, both
Vector
andArrayList
are classes that implement theList
interface, and they are used to store and manipulate collections of objects. - Vector is one of the original collection classes in Java and came into existence since Java 1.0 whereas ArrayList is part of the Java Collections Framework but introduced in Java 1.2. Hence ArrayList is a newer and more commonly used alternative to Vector in Java.
Vector in Java
Definition
- Vector is a separate class in java (import java.util.Vector) that implements the concept of dynamic array in Java(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).
- All the methods in Vector are synchronized (i.e., it is thread-safe), making it suitable for use in multi-threaded environments where multiple threads may modify the list concurrently.
- Vector is still used in few cases because it is thread safe, and used especially in situations where synchronization is necessary.
Advantages
- They provide dynamic array concept i.e. array itself expands or shrinks their size automatically as per requirement in the program.
- Vector is slower than ArrayList in single-threaded environments since it is synchronized because of the overhead associated with acquiring and releasing locks.
- When a Vector reaches its capacity, it automatically increases its size by a certain amount.
- They also support generic array concept i.e. any array cell can be accessed/referenced using normal integer index value of the cell.
- They have many useful predefined methods to do jobs easily.
- It maintains the insertion order of elements as simple array.
- It is better to use in sensitive and specific applications.
Disadvantages
- When using Vector, 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.
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
- The basic operation in ArrayList is very much similar as Vector concept.
- Unlike Vector, ArrayList is not synchronized by default i.e., ArrayList is not thread-safe. It means that if multiple threads access an ArrayList concurrently and at least one of the threads modifies the list structurally (adding or removing elements), then it must be synchronized externally.
- 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.
- ArrayList when required increases its size by 50% of the current capacity.
- At present, ArrayList is preferred over Vector due to its better performance in single-threaded environments and its compatibility with the modern Java Collections Framework.
Advantages
- Similar as basic vector concept.
- ArrayList is faster than Vector in single-threaded environments since it is not synchronized. However, in multi-threaded environments, the performance difference may not be significant.
Disadvantages
- Similar as basic vector concept.
Click this link to see the difference between Vector and ArrayList
Vector Program 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 : In this case, vector itself assign the index with given data automatically from 0.
-------- 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 output of Vector class before cleaning is " + vc);
vc.clear();
System.out.println("After clearing the Vector we have : " + vc);
}
}
Output:
The output of Vector class before cleaning 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 in the form of String 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 all 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 all 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
ArrayList Program Examples
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
0 Comments