Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

vector
size
array
communitycreator

What is the vector.size method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The size method of the Vector class can be used to get the number of components in this vector.


Vector is a growable array of objects. Elements are mapped to an index.


Syntax


public int size();

Code

import java.util.Vector;
class VectorSizeExample {
public static void main( String args[] ) {
// Creating Vector
Vector<Integer> vector = new Vector<>();
System.out.println("Vector is: " + vector);
System.out.println("Vector Size: " + vector.size());
// add elememts
vector.add(1);
vector.add(2);
vector.add(3);
// Print vector
System.out.println("Vector is: " + vector);
System.out.println("Vector Size: " + vector.size());
}
}

Explanation

  • In the code above, we have created an empty Vector object and printed the size of the vector.

  • The empty Vector object’s size is 0 because it doesn’t contain any elements. We then added 3 values 1, 2, 3 using the add method.

  • We again printed the size of the Vector object using the size method. Now the size method returns 3.

RELATED TAGS

vector
size
array
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring