Monday 25 May 2015

How to count number of element in vector

Size() Method return number of element in the vector.

import java.util.Vector;

public class CountElement {
   
    public static void main(String[] args) {
      
        Vector<String> vector = new Vector<String>();
        //add element in vector
        vector.add("cricket");
        vector.add("hockey");
        vector.add("football");
        vector.add("tennish");
      
        // print vector
        System.out.println("Vector is = "+vector);
      
            /*
             * Returns the number of components in this vector.
             */
        System.out.println("size = "+vector.size());
    }

}

Output:

Vector is = [cricket, hockey, football, tennish]

size = 4
 

No comments:

Post a Comment