Java Vector isEmpty() Method



Description

The Java Vector isEmpty() method is used to tests if this vector has no components.

Declaration

Following is the declaration for java.util.Vector.isEmpty() method

public boolean isEmpty()

Parameters

NA

Return Value

The return value is true if and only if this vector has no components, that is, its size is zero.Otherwise it returns false.

Exception

NA

Checking Emptyness of a Vector of Integer Example

The following example shows the usage of Java Vector isEmpty() method. Here we are working with an Vector of Integers. At first, we initialize an Vector object and then check if it is empty or not. Then we'll be adding few elements and then check again if Vector object is empty or not.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty array list 
      Vector<Integer> vector = new Vector<>();

      if (vector.isEmpty()) {
         System.out.println("vector is empty");
      } else {
         System.out.println("vector is not empty");
      }

      // printing all the elements available in vector
      System.out.println("Vector = " + vector);
	  
      // use add() method to add elements in the vector
      vector.add(0);
      vector.add(1);
      vector.add(2);
      vector.add(3);
      vector.add(4);
      vector.add(5);
      vector.add(6);
	
      if (vector.isEmpty()) {
         System.out.println("vector is empty");
      } else {
         System.out.println("vector is not empty");
      }

      // printing all the elements available in vector
      System.out.println("Vector = " + vector);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

vector is empty
Vector = []
vector is not empty
Vector = [0, 1, 2, 3, 4, 5, 6]

Checking Emptyness of a Vector of Integer Example

The following example shows the usage of Java Vector isEmpty() method. Here we are working with an Vector of Strings. At first, we initialize an Vector object and then check if it is empty or not. Then we'll be adding few elements and then check again if Vector object is empty or not.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty array list
      Vector<String> vector = new Vector<>();

      if (vector.isEmpty()) {
         System.out.println("vector is empty");
      } else {
         System.out.println("vector is not empty");
      }

      // printing all the elements available in vector
      System.out.println("Vector = " + vector);

      // use add() method to add elements in the vector
      vector.add("A");
      vector.add("B");
      vector.add("C");
	  
      if (vector.isEmpty()) {
         System.out.println("vector is empty");
      } else {
         System.out.println("vector is not empty");
      }

      // printing all the elements available in vector
      System.out.println("Vector = " + vector);     
   }
}

Output

Let us compile and run the above program, this will produce the following result −

vector is empty
Vector = []
vector is not empty
Vector = [A, B, C]

Checking Emptyness of a Vector of Integer Example

The following example shows the usage of Java Vector isEmpty() method. Here we are working with an Vector of Student objects. At first, we initialize an Vector object and then check if it is empty or not. Then we'll be adding few elements and then check again if Vector object is empty or not.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create an empty vector
      Vector<Student> vector = new Vector<>();
      
      if (vector.isEmpty()) {
         System.out.println("vector is empty");
      } else {
         System.out.println("vector is not empty");
      }

      // printing all the elements available in vector
      System.out.println("Vector = " + vector);
      // use add() method to add elements in the vector
      vector.add(new Student(1, "Julie"));
      vector.add(new Student(2, "Robert"));
      vector.add(new Student(3, "Adam"));

      if (vector.isEmpty()) {
         System.out.println("vector is empty");
      } else {
         System.out.println("vector is not empty");
      }

      // printing all the elements available in vector
      System.out.println("Vector = " + vector);     
   }
}

class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

vector is empty
Vector = []
vector is not empty
Vector = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_vector.htm
Advertisements