Implement a Vector that accepts five items from the command line and store them in a Vector and display the objects stored in a Vector
The Vector class implements a growable array of objects. Like an array, it contains elements that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created. It has methods such as capacity( ), size( ), addElement( ), copyInto( ), contains( ), etc. We are providing vector elements using command line arguments and store them in a vector v using addElement( ) method as shown in program. After that we have copied all the vector elements in String array and display them on console.
PROGRAM
The Vector class is present in java.util package. The syntax for creating a vector is shown below:
// Creates a vector with initial size 10
Vector <vector-name> = new Vector();
OR
// Creates a vector with specified initial capacity.
Vector <vector-name> = new Vector(initialCapacity);
OR
// Creates a vector with specified initial capacity and capacity increment.
Vector <vector-name> = new Vector(initialCapacity, capacityIncrement);
PROGRAM
import java.lang.*;
import java.io.*;
import java.util.*;
public class VectorDemo {
public static void main(String args[]) {
Vector list = new Vector();
int len=args.length;
for(int i=0;i<len;i++) {
list.addElement(args[i]);
}
int size=list.size();
String str[]= new String[size];
list.copyInto(str);
for(int i=0;i<size;i++) {
System.out.println ("Element of Vector at position "+i+":"+str[i]);
}
}
}
OUTPUT
C:\>javac VectorDemo.java C:\>java VectorDemo 12 Rahul a 15.2 Element of Vector at position 0:12 Element of Vector at position 1:Rahul Element of Vector at position 2:a Element of Vector at position 3:15.2
not run properly
ReplyDeleteYou have to pass command line arguments
DeleteC:\>javac VectorDemo.java
C:\>java VectorDemo 12 Rahul a 15.2
Here "12" is integer
"Rahul" is a String
"a" is character and
"15.3" is float
Regularly, when our clients submit craftsmanship for printing, we find that they have no clue about what a Vector record is.Professional graphic design
ReplyDelete