Vector in java

Vector
  • Vector is implementation class of list interface
  • Vector implements three marker interface
    — Serializable
    — Clonable
    — Random access
  • Vector is a legacy class which is introduced in java 1.0
  •  All method in vector is synchronized so it is thread safe class.
  • Whenever we want to achieve thread safety we go through vector.
  •  Vector size grows by doubling the capacity.
Example of Vector
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
package collectionConcept;
import java.util.Vector;
 
public class Vector1
 {
   public static void main(String[] args)
  {
    System.out.println("*************************");
 
    Vector a =new Vector();
    a.add("abhilash");
    a.add("bhavesh");
    a.add("rohit");
    a.add("hardik");
    a.add("jspiders");
 
    System.out.println(a.size());
 
   //setting value at index 4
   a.set(4, "abhilash");
 
    for (int i = 0; i < a.size(); i++)
     {
 
        System.out.println(a.get(i));
     }
 
  }
 
}
output:
 
*************************
5
abhilash
bhavesh
rohit
hardik
abhilash

Leave a Reply

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