0% found this document useful (0 votes)
11 views11 pages

Vector

Uploaded by

yodala159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views11 pages

Vector

Uploaded by

yodala159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

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 java.util.Collections 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
– http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html
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 java.util.Vector; // “import” is like “using namespace” in C#, C++
or
import java.util.*;
• 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.
System.out.println(v1.toString());
import java.util.*;
class GenericPair {
public static void main(String[] args) {
Vector v =new Vector(3,2);
System.out.println("initial size:"+ v.size());
System.out.println("Initial capacity:"+ v.capacity());
v.addElement(new Integer(1));
v.addElement(new Integer(2));
v.addElement(new Integer(3));
v.addElement(new Integer(4));
v.addElement(new Integer(5));
System.out.println("Capacity after additions:"+ v.capacity());
v.addElement(new Double(59.4));
System.out.println("Current Capacity:"+v.capacity() );
System.out.println("First element:"+(Integer)v.firstElement());
if(v.contains(new Integer(3)))
System.out.println("Vector contains 3.");
Enumeration vEnum=v.elements();
while(vEnum.hasMoreElements())
System.out.print(vEnum.nextElement() + " "); }}

---------- 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

You might also like