Vector in Java

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Vector in Java

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.

Declaration:
public class Vector<E> extends AbstractList<E> implements
List<E>, RandomAccess, Cloneable, Serializable

Here, E is the type of element.


 It extends AbstractList and implements List interfaces.

 It implements Serializable, Cloneable, Iterable<E>,


Collection<E>, List<E>, RandomAccess interfaces.

 The directly known subclass is Stack.

Constructors:

1. Vector(): Creates a default vector of the initial capacity is 10.


Vector<E> v = new Vector<E>();
2. Vector(int size): Creates a vector whose initial capacity is
specified by size.
Vector<E> v = new Vector<E>(int size);
3. Vector(int size, int incr): Creates a vector whose initial
capacity is specified by size and increment is specified by incr.
It specifies the number of elements to allocate each time that a
vector is resized upward.
Vector<E> v = new Vector<E>(int size, int incr);
4. Vector(Collection c): Creates a vector that contains the
elements of collection c.
Vector<E> v = new Vector<E>(Collection c);

// Java program to demonstrate the


// working of Vector
  
import java.io.*;
import java.util.*;
  
class VectorExample {
    
    public static void main(String[] args)
    {
        // Size of the
        // Vector
        int n = 5;
  
        // Declaring the Vector with
        // initial size n
        Vector<Integer> v = new
Vector<Integer>(n);
  
        // Appending new elements at
        // the end of the vector
        for (int i = 1; i <= n; i++)
            v.add(i);
  
        // Printing elements
        System.out.println(v);
  
        // Remove element at index 3
        v.remove(3);
  
        // Displaying the vector
        // after deletion
        System.out.println(v);
  
        // Printing elements one by one
        for (int i = 0; i < v.size(); i++)
            System.out.print(v.get(i) + "
");
    }
}

Output
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5
Vectors

The key difference between Arrays and Vectors in Java is that


Vectors are dynamically-allocated. They aren't declared to contain a
type of variable; instead, each Vector contains a dynamic list of
references to other objects. The Vector class is found in the java.util
package, and extends java.util.Abstractlist.

The big advantage of using Vectors is that the size of the vector can
change as needed. Vectors handle these changes through the
"capacity" and "capacityIncrement" fields. When a Vector is
instantiated, it declares an object array of size initialCapacity.
Whenever this array fills, the vector increases the total buffer size by
the value capacityIncrement. Thus, a Vector represents a
compromise. It doesn't allocate each object dynamically, which
would make access of middle list-items exceedingly slow; but it does
allow for growth in the size of the list. The default constructor,
Vector(), creates an empty Vector of initial capacity zero that doubles
in size whenever it fills.

Vectors can be filled using its method addElement(Object obj). This


method appends the given object to the end of the Vector. Because
the Vector stores pointers to the objects, and not the objects
themselves, these Vector items could be any kind of object.
Moreover, several different kinds of objects could exist in the same
Vector. Vector methods allow all sorts of nifty ways to manipulate
data. For example, one can reference a list item not just by its index,
but also by name. Elements can be removed from or added to the
middle of the list:
myVector.insertElementAt(my_object, 5);
success_flag = myVector.remove(my_object);

So you're thinking, "GREAT! But if vectors are so wonderful, why


even bother with arrays?"

Comparative Advantages of Arrays and Vectors

Because Vectors are dynamically-allocated, they offer a relatively memory-


efficient way of handling lists whose size could change drastically. The line
buffer of a text editor, for example, could be anywhere from zero to thousands
of entries in size. A Vector might be a good choice for such an arrangement.
Moreover, Vectors have some useful member functions that make coding
simpler (see the insertElementAt and remove methods above). But because
the Vector class is based on an array of object references, these methods are
generally no more efficient than array-based algorithms. The insert method
must perform multiple "swap" operations just as an array 'insert' algorithm
would. Thus, Vectors are easier to use than arrays for most applications, but
they do not offer all the performance advantages of fully-dynamic storage.

You might also like