Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

vector
javascript
communitycreator

What is the Vector.isEmpty() 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 isEmpty method of the Vector class can be used to check if the vector object does not contain any elements.

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

Syntax

public boolean isEmpty();

We can call the isEmpty method on any Vector object.

Parameters

This method does not take an argument.

Return value

This method returns true if the vector does not contain any elements. Otherwise, it returns false.

Code

import java.util.Vector;
class VectorIsEmptyExample {
public static void main( String args[] ) {
// Creating Vector
Vector<Integer> vector = new Vector<>();
// Print vector
System.out.println("The Vector is: " + vector);
System.out.println("Is Vector Empty: " + vector.isEmpty());
// add elememts
vector.add(1);
// Print vector
System.out.println("The Vector is: " + vector);
System.out.println("Is Vector Empty: " + vector.isEmpty());
}
}

Explanation

In the code above, we create a vector object. Initially, the vector doesn’t contain any elements, so the isEmpty method returns true when we call vector.isEmpty(). Then, we add one element to the vector object. When we call the isEmpty method, it now returns false because the vector object is not empty.

RELATED TAGS

vector
javascript
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