• Java 資料結構教程

驗證 BitSet 是否為空



當 BitSet 中的所有值都為假時,它被認為是空的。BitSet 類提供了 isEmpty() 方法。此方法返回一個布林值,噹噹前 BitSet 為空時返回 false,不為空時返回 true。

您可以使用 isEmpty() 方法驗證特定的 BitSet 是否為空。

示例

import java.util.BitSet;
public class isEmpty {
   public static void main(String args[]) {
      BitSet bitSet = new BitSet(10);  
      
      for (int i = 1; i<25; i++) {
         if(i%2==0) {
            bitSet.set(i);
         }      
      }
      
      if (bitSet.isEmpty()) {
         System.out.println("This BitSet is empty");    
      } else {
         System.out.println("This BitSet is not empty"); 
         System.out.println("The contents of it are : "+bitSet);
      }
      bitSet.clear();
      
      if (bitSet.isEmpty()) {
         System.out.println("This BitSet is empty");    
      } else {
         System.out.println("This BitSet is not empty"); 
         System.out.println("The contents of it are : "+bitSet);
      }    
   }
}

輸出

This BitSet is not empty
The contents of it are : {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24}
This BitSet is empty
廣告

© . All rights reserved.