How to add and get elements in java vector class?
Previous | Home | Next |
This example will shows how to add element to the vector class and how to get the element from the vector class.
To add element to the Vector class use boolean add(Object o) method and to get the element use get() method.
Two methods are used in this example Add() to add element s and get() to display the element of the Vector class.
Create the object of the element . and then call the add method with parameters.
package Example;
import java.util.Vector;
public class VectorExample {
/**
* @param args
*/
public static void main(String[] args) {
Vector vr=new Vector();
vr.add("10");
vr.add("20");
vr.add("30");
vr.add("40");
vr.add("50");
System.out.println("The elements in vector are :");
//to get the values use get method of vector class
System.out.println(vr.get(0));
System.out.println(vr.get(1));
System.out.println(vr.get(2));
System.out.println(vr.get(3));
// TODO Auto-generated method stub
}
}
The elements in vector are :
10
20
30
40
Previous | Home | Next |