Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Collection Vector in java

Related Tutorials


 
 

Share on Google+Share on Google+

Vector in java

Advertisement
Vector in java implements dynamic array. It is similar to array and the component of vector is accessed by using integer index. Size of vector can grow or shrink as needed, by adding and removing item from vector.

Vector in java

Vector in java implements dynamic array. It is similar to array and the component of vector is accessed by using integer index. Size of vector can grow or shrink as needed, by adding and removing item from vector. Vector is very useful when we don't know the size in advance. All the vector start with initial capacity,  if any object is added then vector will allocate space for that object with one extra space for additional object.. Vector class support  four constructors they are as follows:

Vector() 

It will create a empty vector with initial size of 10.

Vector(int size)
This form will create a vector with initial capacity is specified by size.
Vector(int size, int incr)

This form will create a vector with initial capacity specified by size and the increment is specified by incr.

Vector(Collection c)

This form will create a vector that contain the element specified in collection.

Here are some of the methods of vector as follows:

MethodsDescription
void add(int index, Object element) Insert the element at specified position
boolean add(Object o)Add the element at the end of the vector
boolean addAll(Collection c)Add all of the element in the collection to the end of the vector.
boolean addAll(int index, Collection c)Add all of the element in the collection at the specified position in the vector
int capacity()Return the current capacity of the vector.
void clear()Remove all element in the vector.
boolean contains(Object element)Check the vector contain the specified object
boolean containsAll(Collection c)Return true if the vector contains all the element in the collection c.
boolean isEmpty()Check whether the vector is empty
Object lastElement()Return the last element of the vector.
boolean remove(Object o)Remove the first occurrence of the specified element in the vector
int size()Return the size of the vector.

Example : The following program to illustrate the use of vector:

import java.util.Enumeration;
import java.util.Vector;

 public class VectorDemo 
  {
	public static void main(String args[])
	{
		 {
		        // Create a Vector and add some elements to it.
		        Vector v = new Vector();
		        v.add("123");
		        v.add("456");
		        v.add("789");

		        // Change the element at index 1.
		        v.setElementAt("0", 1);

		        // Display the contents of the Vector.
		        Enumeration Enum = v.elements();
		        while (Enum.hasMoreElements())
		        {
		            System.out.println(Enum.nextElement());
		        }
		    }
		}
	   }

Output : After compiling and executing the program

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 15, 2013

Related Tutorials

Discuss: Vector in java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com