沈阳做一个网站需要多少钱,一个完整的企业策划案范文,中国制造网外贸网官网登录,图派做网站写个小东西#xff0c;要去重复数字#xff0c;用到BIT数组#xff0c;虽然JAVA已经提供了一个BitSet#xff0c;不过自己手痒#xff0c;又写了一个简单的 原理就不写了#xff0c;网上一大堆 1 import java.util.Iterator;2 import java.util.function.BiConsumer;3 4 …写个小东西要去重复数字用到BIT数组虽然JAVA已经提供了一个BitSet不过自己手痒又写了一个简单的 原理就不写了网上一大堆 1 import java.util.Iterator;2 import java.util.function.BiConsumer;3 4 public class BitArray implements IterableBoolean{5 //表示1n的值提高效率不用每次计算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 i0; ibits.length; i){
50 byte b bits[i];
51 for(int bitIndex0; bitIndex8;bitIndex){
52 System.out.print( ((bbitIndex) 1) );
53 }
54 System.out.println();
55 }
56 }
57
58 /**
59 * 提供遍历接口
60 */
61 public IteratorBoolean iterator() {
62 return new IteratorBoolean(){
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(BiConsumerInteger,Boolean fun){
81 int total 0;
82 for(int i0; ibits.length; i){
83 byte b bits[i];
84 for(int bitIndex0; bitIndex8 totalmax;bitIndex,total){
85 fun.accept(total, ((bbitIndex) 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(--------------------------);//遍历方式二BiConsumerInteger,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
*/ 转载于:https://www.cnblogs.com/varlxj/p/5168157.html