java中的BitSet學習

來源:互聯網
上載者:User

(1)BitSet類
    大小可動態改變, 取值為true或false的位集合。用於表示一組布爾標誌。   

此類實現了一個按需增長的位向量。位 set 的每個組件都有一個 boolean 值。用非負的整數將 BitSet 的位編入索引。可以對每個編入索引的位進行測試、設定或者清除。通過邏輯與、邏輯或和邏輯異或操作,可以使用一個 BitSet 修改另一個 BitSet 的內容。

預設情況下,set 中所有位的初始值都是 false。

  每個位 set 都有一個當前大小,也就是該位 set 當前所用空間的位元。注意,這個大小與位 set 的實現有關,所以它可能隨實現的不同而更改。位 set 的長度與位 set 的邏輯長度有關,並且是與實現無關而定義的。

   除非另行說明,否則將 null 參數傳遞給 BitSet 中的任何方法都將導致 NullPointerException。 在沒有外部同步的情況下,多個線程操作一個 BitSet 是不安全的。

(2) 建構函式: BitSet() or BitSet(int nbits)

(3) 一些方法
public void set(int pos): 位置pos的字位設定為true。
public void set(int bitIndex, boolean value) 將指定索引處的位設定為指定的值。
public void clear(int pos): 位置pos的字位設定為false。
public void clear() : 將此 BitSet 中的所有位設定為 false。
public int cardinality() 返回此 BitSet 中設定為 true 的位元。
public boolean get(int pos): 返回位置是pos的字位值。
public void and(BitSet other): other同該字位集進行與操作,結果作為該字位集的新值。
public void or(BitSet other): other同該字位集進行或操作,結果作為該字位集的新值。
public void xor(BitSet other): other同該字位集進行異或操作,結果作為該字位集的新值。
public void andNot(BitSet set) 清除此 BitSet 中所有的位,set - 用來屏蔽此 BitSet 的 BitSet
public int size(): 返回此 BitSet 表示位值時實際使用空間的位元。
public int length() 返回此 BitSet 的“邏輯大小”:BitSet 中最高設定位的索引加 1。
public int hashCode(): 返回該集合Hash 碼, 這個碼同集合中的字位值有關。
public boolean equals(Object other): 如果other中的字位同集合中的字位相同,返回true。
public Object clone() 複製此 BitSet,產生一個與之相等的新 BitSet。
public String toString() 返回此位 set 的字串表示形式。

例1:標明一個字串中用了哪些字元

import java.util.BitSet;public class WhichChars{   private BitSet used = new BitSet();   public WhichChars(String str){      for(int i=0;i< str.length();i++)        used.set(str.charAt(i));  // set bit for char   }    public String toString(){         String desc="[";         int size=used.size();          for(int i=0;i< size;i++){             if(used.get(i))                 desc+=(char)i;            }             return desc+"]";         }    public static void main(String args[]){        WhichChars w=new WhichChars("How do you do");        System.out.println(w);    }   }

運行:
C:\work>java WhichChars
[ Hdouwy]

例2:篩選法求素數

import java.util.*;public class BitSetTest{   public static void main(String[] args){      BitSet sieve=new BitSet(1024);      int size=sieve.size();      for(int i=2;i< size;i++)           sieve.set(i);      int finalBit=(int)Math.sqrt(sieve.size());            for(int i=2;i< finalBit;i++)         if(sieve.get(i))           for(int j=2*i;j< size;j+=i)               sieve.clear(j);            int counter=0;      for(int i=1;i< size;i++){          if(sieve.get(i)){             System.out.printf("%5d",i);             if(++counter%15==0)                System.out.println();          }       }    }}C:\work>java   BitSetTest    2    3    5    7   11   13   17   19   23   29   31   37   41   43   47   53   59   61   67   71   73   79   83   89   97  101  103  107  109  113  127  131  137  139  149  151  157  163  167  173  179  181  191  193  197  199  211  223  227  229  233  239  241  251  257  263  269  271  277  281  283  293  307  311  313  317  331  337  347  349  353  359  367  373  379  383  389  397  401  409  419  421  431  433  439  443  449  457  461  463  467  479  487  491  499  503  509  521  523  541  547  557  563  569  571  577  587  593  599  601  607  613  617  619  631  641  643  647  653  659  661  673  677  683  691  701  709  719  727  733  739  743  751  757  761  769  773  787  797  809  811  821  823  827  829  839  853  857  859  863  877  881  883  887  907  911  919  929  937  941  947  953  967  971  977  983  991  997 1009 1013 1019 1021

例3:簡單使用

import java.util.BitSet;public class BitOHoney {  public static void main(String args[]) {    String names[] = { "Java", "Source", "and", "Support"};    BitSet bits = new BitSet();    for (int i = 0, n = names.length; i < n; i++) {      if ((names[i].length() % 2) == 0) {        bits.set(i);      }    }    System.out.println(bits);    System.out.println("Size : " + bits.size());    System.out.println("Length: " + bits.length());    for (int i = 0, n = names.length; i < n; i++) {      if (!bits.get(i)) {        System.out.println(names[i] + " is odd");      }    }    BitSet bites = new BitSet();    bites.set(0);    bites.set(1);    bites.set(2);    bites.set(3);    bites.andNot(bits);    System.out.println(bites);  }}

運行:
C:\work>java BitOHoney
{0, 1}
Size : 64
Length: 2
and is odd
Support is odd
{2, 3}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.