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. 

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();


// Creates a vector with specified initial capacity.
Vector  <vector-name> = new  Vector(initialCapacity);


// Creates a vector with specified initial capacity and capacity increment.
Vector  <vector-name> = new  Vector(initialCapacity,  capacityIncrement);

import java.lang.*;
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++) {
  int size=list.size();
  String str[]= new String[size];
  for(int i=0;i<size;i++) {
   System.out.println ("Element of Vector at position "+i+":"+str[i]);
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

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.