Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (
post your own) View All Comments Latest 10 Comments:

Getting : cannot find main class.program will exit after running below pgm.

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(2, new 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());
}
}
}

Posted by Rajeshwari on Sunday, 06.1.08 @ 11:48am | #61683

hi,
how to add the single string as given below
234.9 435.0 903.1
342.1 874.2 234.1
134.1 341.2 123.2
245.1 243.2 342.6
234.9 230.5 343.7

in a vector .I want to add the above text as single string in a vector.Kindly send me source code for this.
Thank you in advance

Posted by anitha venkat on Monday, 10.1.07 @ 08:13am | #30802

help me find sample program in array..that determines the 8 numbers if it is lost or found

Posted by mightymary on Thursday, 03.15.07 @ 09:04am | #11758

Latest Searches:
Photoshop Photo Effect
ready made component o
java random numbers
count row in resultset
jdbc using prepared st
Horizontal splitpane
link
logiceic:equal
login using JSP
Pattern Testing
Java Pass Value
html code for developi
modity data in text fi
Photoshop Text Effects
validate radio button
drop down in jsp
TextField
flex datagrids
sum of rows and coloum
display
multiple submit button
user registration
jsp bean
java example program t
populate drop down fro
vector in java
Struts Validator Frame
java xml
Get Array List in a JS
Add listener to compon
distinct
game in java
how to pass same varia
jQuery To Hide the Div
rotate
Why Virtual Dedicated
runnable
how to execute update
helloe world servlet
t:htmltag
JSP:File Input Paramet
Check Box (Form Tag) E
FCKediter
2 tier ,3 tier ,n tier
user registration and
pthread.h
where we place plug-in
create table in struts
if not null condition
abstrct path name of f
howtodeclareanarrayinj
JPOX
dynamic insert value J
How to show expression
populate drop down fro
file input output
dwr reverse ajax
b tree implementation
jboss seam
iBatis Stored Procedur
Photoshop Effects Sig
using set in java
date of birth
swing
session jsp
find and replace bytes
Photoshop Color Colori
<c:if
Passing Parameters in
width
ASP EXAMPLE and 1=2
Photoshop Text Effects
Get Image
MULTIPLE SELECT COMBO
dojo debug
call ejb j2ee5
JSF tree collapse
spring MVC application
strating a thread in j
jar entry
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.