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



Home Java Javatutorial Vector in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Vector in Java

Advertisement
Vector in Java are array lists that are used instead of arrays, as they have extending properties, which means that they can grow and shrink to accommodate data even after the Vector has been created. Capacity of vector is always to the lowest degree. Size of the vector is however larger because components are added to the vector at run-time.

Vector in Java are array lists that are used instead of arrays, as they have extending properties, which means that they can grow and shrink to accommodate data even after the Vector has been created.

Capacity of vector is always to the lowest degree. Size of the Vector is however larger because components are added to the vector at run-time.

Integer index access the component in vector class. Vectors implement the Enumeration interface, which makes the contents of a Vector traversed.

Vector instances can grow dynamically. If they reach the maximum limit, resources are allocated dynamically and size gets increased further.

Vector maintains its storage size by capacity Increment.

Here is how to declare a Vector in Java Program:

This Syntax is used to declare an empty Vector r (internal data array size-10, standard storage capacity-0):

public Vector()

This Syntax constructs an empty vector specified by initial capacity:

vector(int initial capacity)

This Syntax creates an empty vector with the specified initial capacity and capacity increment:

vector(int initial capacity,int capacity increment)

Following are the Methods used in Vector:

  1. void add(int index, Object element): This method inserts a specific element at a specific position.
  2. boolean add(object o): This method Appends a specific element to the end of the Vector.
  3. int  capacity( ):  This method returns the current capacity of the vector
  4. void clear( ): This method removes all of the elements from the Vector
  5. void setsize(int new size): This method sets the new size of Vector.
  6. int size( ): This method returns the number of Component in Vector.
  7. Vector trim to size( ): This method trims the Capacity of Vector to vector's present size.
  8. public int lastindexof(obj abc): This method returns the occurrence of last index of the specified object in this vector.

Vector java Example:

package Toturial;

import java.util.Iterator;
import java.util.Vector;

public class VectorExample {
	public static void main(String[] args) {
		Vector vt = new Vector();
		vt.add("A");
		vt.add("RoseIndia");
		vt.add("Hello,Hi");
		vt.add(123);
		vt.add(123.45);
		vt.removeElement("A"); // We have create removeElement method

		Iterator it = vt.iterator();
		while (it.hasNext())
			System.out.println(it.next());

	}

      }

OutPut:-

RoseIndia

Hello,Hi

123

123.45

Advertisement

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: April 29, 2013

Related Tutorials

Discuss: Vector in Java  

Post your Comment


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