发表于: java/j2ee | 作者: | 日期: 2014/1/28 09:01
标签:

在使用PAT-Tree的中文实现中需要用到大量的位运算,BitSet在效率上的表现可能尚待商榷,不过拿来做实验是没有问题的。
在Java的文档中说明了BitSet实现了Vector接口,是一个可按需增长的存储位的数据结构,位的值位布尔型,初始大小为64bit,初始值均为“false”。
常用方法:
void set(int bitIndex)、void set(int bitIntex, boolean value)
将bitIndex位置的值设为“true”或者给定的值
boolean get(int bitIndex)
获取bitIndex位置处的值
BitSet get(int fromIndex, int toIndex)
以BitSet存储返回fromIndex与toIndex区间之间的值
void clear()、void clear(int bitIndex)、void clear(int fromIndex, int toIndex)
置整个BitSet/相应位置/区间的值为“false”
int length() :
BitSet的逻辑大小(实际使用大小),值为“true”最大的索引位加1
int size() BitSet的实际大小,默认为64
and(BitSet set)、andNot(BitSet set) … :
与另一BitSet相应的逻辑位运算
String toString() :
返回值为“true”的位置的String对象,如第1和第10位为“true”,其他位置为“false”,则返回{1, 10}
应用实例:下面的代码将字符转换为与编码对应的二进制字符串:
Java代码
package org.eone.test;
import java.util.BitSet;
public class TestBitSet {
private static String toBitSet(String str){
String result = “[“;
BitSet bitSet = new BitSet();
byte[] strBits = str.getBytes();
for(int i = 0; i< strBits.length * 8; i++){ if((strBits[i / 8] & (128 >> (i % 8))) > 0){
bitSet.set(i);
}
}
for(int i = 0; i < bitSet.length(); i++){ if(bitSet.get(i)) result += "1"; else result +="0"; } result += "]"; return result; } public static void main(String[] args) { String str = "测试一下"; System.out.println(toBitSet(str)); } } 使用时还需注意,在多线程情况下要注意线程安全问题。 [来源:http://haiker.iteye.com/blog/1742492]

: https://blog.darkmi.com/2014/01/28/3641.html

本文相关评论 - 1条评论都没有呢
Post a comment now » 本文目前不可评论

No comments yet.

Sorry, the comment form is closed at this time.