What is Vector class ?
Vector class is predefined class in java.This class is present inside the java.util package.
Vector is similar like Array but having some difference between them.
Vector class and their method
//Write a program to add some element into the Vector and remove any element,Display the result.
//Example of Vector class.
import java.util.*;
class VectorDemo
{
public static void main(String[] args)
{
Vector v=new Vector();
int a=args.length;
for(int i=0;i<a;i++)
{
v.addElement(args[i]);
}
int size=v.size();
System.out.println("Initial Size of Vector:"+size);
String b[]=new String[size];
v.copyInto(b);
for(int i=0;i<size;i++)
{
System.out.println("Elements of Vector:"+i+":"+b[i]);
}
v.removeElementAt(2);
int s=v.size();
v.copyInto(b);
System.out.println("After Removing Element,Size of Vector:"+s);
for(int i=0;i<s;i++)
{
System.out.println("Elements of Vector:"+i+":"+b[i]);
}
}
}
Vector class is predefined class in java.This class is present inside the java.util package.
Vector is similar like Array but having some difference between them.
- Vector is having many predefined method as array dont have it.
- Vector is synchronized.
- Vector is very useful when if you dont know the size of array earlier or you have to change the size over the lifetime of your program.
Vector class and their method
//Write a program to add some element into the Vector and remove any element,Display the result.
//Example of Vector class.
import java.util.*;
class VectorDemo
{
public static void main(String[] args)
{
Vector v=new Vector();
int a=args.length;
for(int i=0;i<a;i++)
{
v.addElement(args[i]);
}
int size=v.size();
System.out.println("Initial Size of Vector:"+size);
String b[]=new String[size];
v.copyInto(b);
for(int i=0;i<size;i++)
{
System.out.println("Elements of Vector:"+i+":"+b[i]);
}
v.removeElementAt(2);
int s=v.size();
v.copyInto(b);
System.out.println("After Removing Element,Size of Vector:"+s);
for(int i=0;i<s;i++)
{
System.out.println("Elements of Vector:"+i+":"+b[i]);
}
}
}
Save this program as VectorDemo.java
Compile it
javac VectorDemo.java
Run it
java VectorDemo 4 5 8 9
Output
Compile it
javac VectorDemo.java
Run it
java VectorDemo 4 5 8 9
Output