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

Leave a Reply

Your email address will not be published. Required fields are marked *

97 − = 87