• Java 数据结构教程

验证向量是否为空



java.util 包中的 Vector 类提供了一个 isEmpty() 方法。此方法验证当前向量是否为空。如果给定的向量为空,则此方法返回 true,否则返回 false。

示例

import java.util.Vector;

public class Vector_IsEmpty {
   public static void main(String args[]) {
      Vector vect = new Vector();
      vect.addElement("Java");
      vect.addElement("JavaFX");
      vect.addElement("HBase");
      vect.addElement("Neo4j");
      vect.addElement("Apache Flume");
      System.out.println("Elements of the vector :"+vect);
      boolean bool1 = vect.isEmpty();
      
      if(bool1==true) {
         System.out.println("Given vector is empty");
      } else {
         System.out.println("Given vector is not empty");    	  
      }
      vect.clear();
      boolean bool2 = vect.isEmpty();
      System.out.println("cleared the contents of the vector");       
      
      if(bool2==true) {
         System.out.println("Given vector is empty");
      } else {
         System.out.println("Given vector is not empty");    	  
      }  
   }
}

输出

Elements of the vector :[Java, JavaFX, HBase, Neo4j, Apache Flume]
Given vector is not empty
cleared the contents of the vector
Given vector is empty
广告