Vector
Kuldeep Kumar Yogi
Banasthali University
What is a Vector object?
• Think of it as a really convenient array
• Consider:
• What if we now want to add a 6th element?
• What if we want to check to see if a particular element is
contained in the array?
• What if we want to sort the elements?
The nice things about Vector
• Vector allows you to add elements “on-
the-fly”
• Vector provides a method for automatically
searching for a particular element in the
array
• The [Link] class provides a
static method for sorting the elements in
the array
Java class Vector
• The Vector class implement a
growable array of objects.
– User can use integer index to access items in a vector, like an
array.
– A vector can expand or shrink as needed when add or delete
items.
• Online document
– [Link]
Fields of class Vector
– elementData
• Internal data array storing elements in the
vector
– elementCount
• The number of elements stored in the
vector. It is equal to the vector’s size.
– capacityIncrement
• The amount by which the capacity of the
vector is automatically incremented when it
becomes full.
Define objects of Vector
• Before defining objects of Vector, import it.
import [Link]; // “import” is like “using namespace” in C#, C++
or
import [Link].*;
• Define Vector objects, use different constructors
Vector v1 = new Vector();
// v1 is empty. Its initial capacity is 10 and capacityIncrement is 0.
Vector v2 = new Vector(50);
// v2 is empty. Its initial capacity is 50 and capacityIncrement is 0.
Vector v3 = new Vector (80, 5);
// v3 is empty. Its initial capacity is 80 and capacityIncrement is 5.
Methods in Vector
• Add elements into a vector
– void add(int index, Object element)
• Add “element” at position “index”
– boolean add(Object o)
• Append “o” at the end of the vector.
Methods in Vector
• Remove elements from a Vector
– boolean remove(Object obj)
• Remove the first occurrence of “obj” from the
vector
• Return false if “obj” is not found.
– Object remove(int index)
• Remove element at location “index” and return it.
Methods in Vector
• Search elements in an vector
– int indexOf(Object elem)
• Search for the first occurrence of “elem” and return its index.
– int indexOf(Object elem, int index)
• Searches for the first occurence of “elem”, beginning the
search at “index”.
– Both return -1 if “elem” is not found.
– More
• int lastIndexOf(Object elem)
• int lastIndexOf(Object elem, int index)
Use methods in Vector
• Vector class provide lots of useful methods
– int size()
– Object set(int index, Object element)
– Object get(int index)
– void clear()
– // Much more…
• Make good use them will make your coding
much easier.
– For instance,
// Output all elements in vector v1.
[Link]([Link]());
import [Link].*;
class GenericPair {
public static void main(String[] args) {
Vector v =new Vector(3,2);
[Link]("initial size:"+ [Link]());
[Link]("Initial capacity:"+ [Link]());
[Link](new Integer(1));
[Link](new Integer(2));
[Link](new Integer(3));
[Link](new Integer(4));
[Link](new Integer(5));
[Link]("Capacity after additions:"+ [Link]());
[Link](new Double(59.4));
[Link]("Current Capacity:"+[Link]() );
[Link]("First element:"+(Integer)[Link]());
if([Link](new Integer(3)))
[Link]("Vector contains 3.");
Enumeration vEnum=[Link]();
while([Link]())
[Link]([Link]() + " "); }}
---------- Output ----------
initial size:0
Initial capacity:3
Capacity after additions:5
Current Capacity:7
First element:1
Vector contains 3.
1 2 3 4 5 59.4