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
- public int size();
public int size();
Example :
- 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);
- }
- }
- }
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.