Java Vector isEmpty() 方法



描述

Java Vector isEmpty() 方法用于测试此向量是否没有组件。

声明

以下是 java.util.Vector.isEmpty() 方法的声明

public boolean isEmpty()

参数

返回值

当且仅当此向量没有组件(即大小为零)时,返回值为 true。否则返回 false。

异常

检查整数向量的空性示例

以下示例演示了 Java Vector isEmpty() 方法的用法。这里我们使用的是整数向量。首先,我们初始化一个向量对象,然后检查它是否为空。然后我们将添加一些元素,然后再次检查向量对象是否为空。

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);
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

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

检查整数向量的空性示例

以下示例演示了 Java Vector isEmpty() 方法的用法。这里我们使用的是字符串向量。首先,我们初始化一个向量对象,然后检查它是否为空。然后我们将添加一些元素,然后再次检查向量对象是否为空。

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);     
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

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

检查整数向量的空性示例

以下示例演示了 Java Vector isEmpty() 方法的用法。这里我们使用的是 Student 对象向量。首先,我们初始化一个向量对象,然后检查它是否为空。然后我们将添加一些元素,然后再次检查向量对象是否为空。

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);
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

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