Home Java Beginners Vector Example in java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Vector Example in java

Advertisement
In this section, you will learn how to use the java.util.vector class.

Vector Example in java

     

In this example we are going to show the use of java.util.Vector class. We will be creating an object of Vector class and performs various operation like adding, removing etc. Vector class extends AbstractList and implements List, RandomAccess, Cloneable, Serializable. The size of a vector increase and decrease according to the program. Vector is synchronized.

In this example we are using seven methods of a Vector class.

add(Object o): It adds the element in the end of the Vector

size(): It gives the number of element in the vector.

elementAt(int index): It returns the element at the specified index.

firstElement(): It returns the first element of the vector.

lastElement(): It returns  last element.

removeElementAt(int index): It deletes the element from the given index.

elements(): It returns an enumeration of the element

In this example we have also used Enumeration interface to retrieve the value of a vector. Enumeration interface has two methods.

hasMoreElements(): It checks if this enumeration contains more elements or not.

nextElement(): It checks the next element of the enumeration.

Code of this program is given below: 

//java.util.Vector and java.util.Enumeration;

import java.util.*;
public class VectorDemo{
  public static void main(String[] args){
  Vector<Object> vector = new Vector<Object>();
  int primitiveType = 10;
  Integer wrapperType = new Integer(20);
  String str = "tapan joshi";
  vector.add(primitiveType);
  vector.add(wrapperType);
  vector.add(str);
  vector.add(2new Integer(30));
  System.out.println("the elements of vector: " + vector);
  System.out.println("The size of vector are: " + vector.size());
  System.out.println("The elements at position 2 is: " 
  + vector.elementAt(2));
  System.out.println("The first element of vector is: " 
  + vector.firstElement());
  System.out.println("The last element of vector is: " 
  + vector.lastElement
());
  vector.removeElementAt(2);
  Enumeration e=vector.elements();
  System.out.println("The elements of vector: " + vector);
  while(e.hasMoreElements()){
  System.out.println("The elements are: " + e.nextElement());
  }  
  }
}

Output of this example is given below:

C:\Java Tutorial>javac VectorDemo.java 

C:\Java Tutorial>java VectorDemo
the elements of vector: [10, 20, 30, tapan joshi]
The size of vector are: 4
The elements at position 2 is: 30
The first element of vector is: 10
The last element of vector is: tapan joshi
The elements of vector: [10, 20, tapan joshi]
The elements are: 10
The elements are: 20
The elements are: tapan joshi

C:\Java Tutorial>_

Download this example

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: June 7, 2007

Related Tutorials

Ask Questions?    Discuss: Vector Example in java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
shireen
August 19, 2011
how to insert elements in vector ?

where this is correct syntax for inserting element V=inserElementAt(n,m) V is the class object in my program
samreen shaikh
September 14, 2011
java basic

can u explain a simple example of vector class normal and simple.....for only undrstandidng the concept of vector.....plzzzzz
Madhu N
November 1, 2011
java

i want to update my knowledge in java please update new trends
MukhtarAhmad
January 8, 2012
vector

what is it vector?
mukhtarahmad
January 8, 2012
i don't no what is it

what is it vector
Ray
March 16, 2012
Vectors at JAVA

Hi, your code is good but I got a question, How can I fill the Vector by keyboard ?, for example, if I need put in there any notes about any number of students, I tried by Scanner method but it dosen´t work, thanks in advance. Regards
Rohit
March 20, 2012
vector

is it possible to store elements in vector with specified index?
praveen
June 18, 2012
vector

what is actually vector?????can you please explain diagrammatically it like stack and linked-list?
DMCA.com