• JAVA的BIT数组


    写个小东西,要去重复数字,用到BIT数组,虽然JAVA已经提供了一个BitSet,不过自己手痒,又写了一个简单的

    原理就不写了,网上一大堆

     1 import java.util.Iterator;
     2 import java.util.function.BiConsumer;
     3 
     4 public class BitArray implements Iterable<Boolean>{
     5     //表示1<<n的值,提高效率,不用每次计算
     6     private final byte[] MASK = new byte[]{1,2,4,8,16,32,64,(byte)128};
     7     byte[] bits;
     8     int max = 0;
     9     
    10     /**
    11      * 构造一个Bit数组
    12      * @param max 最大位数
    13      */
    14     public BitArray(int max){
    15         this.max = max;
    16         int len = max / 8 + 1;
    17         
    18         bits = new byte[len];
    19     }
    20     
    21     /**
    22      * 设置第N位的值
    23      * @param index Bit索引
    24      * @param value 值
    25      */
    26     public void set(int index,boolean value){
    27         int i = index / 8;
    28         int move = index % 8;
    29         
    30         bits[i] = (byte)(bits[i] | MASK[move]);
    31     }
    32     
    33     /**
    34      * 取得第N位的值
    35      * @param index Bit索引
    36      * @return
    37      */
    38     public boolean get(int index){
    39         int i = index / 8;
    40         int move = index % 8;
    41         
    42         return (bits[i] & MASK[move]) == MASK[move];
    43     }
    44     
    45     /**
    46      * 显示所有位
    47      */
    48     public void show(){
    49         for(int i=0; i<bits.length; i++){
    50             byte b = bits[i];
    51             for(int bitIndex=0; bitIndex<8;bitIndex++){
    52                 System.out.print( ((b>>bitIndex) & 1) + " ");
    53             }
    54             System.out.println();
    55         }
    56     }
    57 
    58     /**
    59      * 提供遍历接口
    60      */
    61     public Iterator<Boolean> iterator() {
    62         return new Iterator<Boolean>(){
    63             private int i = 0;
    64 
    65             public boolean hasNext() {
    66                 return i <= max;
    67             }
    68 
    69             public Boolean next() {
    70                 return get(i++);
    71             }
    72             
    73         };
    74     }
    75     
    76     /**
    77      * 遍历,偷懒用了JAVA8的新接口
    78      * @param fun
    79      */
    80     public void forEach(BiConsumer<Integer,Boolean> fun){
    81         int total = 0;
    82         for(int i=0; i<bits.length; i++){
    83             byte b = bits[i];
    84             for(int bitIndex=0; bitIndex<8 && total<=max;bitIndex++,total++){
    85                 fun.accept(total, ((b>>bitIndex) & 1) == 1);
    86             }
    87         }
    88     }
    89 }

    使用方式:

    public static void main( String[] args ) throws Exception
    {
        BitArray bits = new BitArray(18);
        bits.set(18,true);
        System.out.println("position 3 : " + bits.get(3));
        System.out.println("position 18 : " + bits.get(18));
        System.out.println("--------------------------");
        
        //遍历方式 一
        int i = 0;
        for(Boolean result : bits)
            System.out.println(i++ + " : " + result);
        
        System.out.println("--------------------------");
        
        //遍历方式二
        BiConsumer<Integer,Boolean> fun = (index, value)->{
            System.out.println(index + " : " + value);
        };        
        bits.forEach(fun);
    }

    输出结果:

    /*
    position 3 : false
    position 18 : true
    --------------------------
    0 : false
    1 : false
    2 : false
    3 : false
    4 : false
    5 : false
    6 : false
    7 : false
    8 : false
    9 : false
    10 : false
    11 : false
    12 : false
    13 : false
    14 : false
    15 : false
    16 : false
    17 : false
    18 : true
    --------------------------
    0 : false
    1 : false
    2 : false
    3 : false
    4 : false
    5 : false
    6 : false
    7 : false
    8 : false
    9 : false
    10 : false
    11 : false
    12 : false
    13 : false
    14 : false
    15 : false
    16 : false
    17 : false
    18 : true
    */
  • 相关阅读:
    cf Round #764(Div. 3)
    查看w3wp.exe 进程
    CAML语法 Query写法
    InfoPaht 复选框
    性能工具MiniProfiler在Asp.Net WebForm跟踪EntityFramework
    CAML基础语法
    Moss 本机无法访问(转)
    STSADM 不是内部或外部命令
    spBodyOnLoadFunctionNames
    关于代码调用SSP获取UserProfile出错的解决方案(转)
  • 原文地址:https://www.cnblogs.com/varlxj/p/5168157.html
Copyright © 2020-2023  润新知