Size of Vector

It is very important to know the size of Vector as it is required in most of the times while coding

We have size() method in Vector class which helps us to determine the size of Vector

Size is the number of elements in that Vector

  1. public int size();
public int size();


Example :

  1. import java.util.*;
  2.  
  3. public class VectorSize {
  4.     public static void main(String[] args) {
  5.     Vector<Integer> vector = new Vector<Integer>();
  6.         vector.add(14);
  7.         vector.add(7);
  8.         vector.add(21);
  9.         vector.add(28);
  10.         vector.add(35);
  11.         vector.add(42);  
  12.         System.out.println("Initial Size of Vector: "+vector.size());
  13.         vector.remove(1);
  14.         vector.remove(2);
  15.         System.out.println("Size after removing 2 elements: "+vector.size());
  16.         System.out.println("Final Vector after removal of 2 elements: ");
  17.         for(int ele: vector){
  18.             System.out.println(ele);
  19.         }
  20.     }
  21. }
import java.util.*;

public class VectorSize {
	public static void main(String[] args) {
	Vector<Integer> vector = new Vector<Integer>();
        vector.add(14);
        vector.add(7);
        vector.add(21);
        vector.add(28);
        vector.add(35);
        vector.add(42);	 
        System.out.println("Initial Size of Vector: "+vector.size());
        vector.remove(1);
        vector.remove(2);
        System.out.println("Size after removing 2 elements: "+vector.size());
        System.out.println("Final Vector after removal of 2 elements: ");
        for(int ele: vector){
            System.out.println(ele);
        }
	}
}
Note : Size just tells how many elements are present in the Vector

Don’t get confuse size with capacity in the Vector, we will talk about capacity separately

Just remember Size() returns number of elements present in Vector

Remember size() is a method , not a variable in the Vector class.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on