Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
93 views

Java Vector: Vector Is Like The Dynamic Array Which Can Grow or Shrink Its Size. Unlike Array, We

Vector implements a dynamic array that can grow or shrink as needed. It is found in the java.util package and implements the List interface. Vectors are similar to ArrayList but are synchronized, contain legacy methods, and maintain insertion order. They are recommended for thread-safe implementations, otherwise an ArrayList may perform better.

Uploaded by

aadi1988
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Java Vector: Vector Is Like The Dynamic Array Which Can Grow or Shrink Its Size. Unlike Array, We

Vector implements a dynamic array that can grow or shrink as needed. It is found in the java.util package and implements the List interface. Vectors are similar to ArrayList but are synchronized, contain legacy methods, and maintain insertion order. They are recommended for thread-safe implementations, otherwise an ArrayList may perform better.

Uploaded by

aadi1988
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Vector

Vector is like the dynamic array which can grow or shrink its size. Unlike array, we
can store n-number of elements in it as there is no size limit. It is a part of Java
Collection framework since Java 1.2. It is found in the java.util package and
implements the List interface, so we can use all the methods of List interface here.

The Vector class implements a growable array of objects. Vectors basically


fall in legacy classes but now it is fully compatible with collections. It is found
in the java.util package and implements the List interface, so we can use all
the methods of List interface here.

 Vector implements a dynamic array that means it can grow or shrink as


required. Like an array, it contains components that can be accessed
using an integer index
 They are very similar to ArrayList but Vector is synchronized and has
some legacy method that the collection framework does not contain.
 It also maintains an insertion order like an ArrayList but it is rarely used
in a non-thread environment as it is synchronized and due to which it
gives a poor performance in adding, searching, delete and update of its
elements.
 The Iterators returned by the Vector class are fail-fast. In the case of
concurrent modification, it fails and throws
the ConcurrentModificationException.

It is recommended to use the Vector class in the thread-safe implementation only.


If you don't need to use the thread-safe implementation, you should use the
ArrayList, the ArrayList will perform better in such case.

The Iterators returned by the Vector class are fail-fast. In case of concurrent


modification, it fails and throws the ConcurrentModificationException.

It is similar to the ArrayList, but with two differences-

o Vector is synchronized.
o Java Vector contains many legacy methods that are not the part of a
collections framework.

Java Vector Example


import java.util.*;  
class VectorExample {  
public static void main(String args[]) {  
//Create a vector  
Vector<String> vec = new Vector<String>();  
//Adding elements using add() method of List  
vec.add("Tiger");  
vec.add("Lion");  
vec.add("Dog");  
vec.add("Elephant");  
//Adding elements using addElement() method of Vector  
vec.addElement("Rat");  
vec.addElement("Cat");  
vec.addElement("Deer");  

System.out.println("Elements are: "+vec);  
}  
}  

You might also like