[英]how to represent an integer array using java.util.BitSet ?
I need to represent an array of integers using BitSet. 我需要使用BitSet表示一个整数数组。 Can somebody explain me the logic required to do this ? 有人能解释一下这样做的逻辑吗?
You can represent a set of integers using BitSet
, but not an arbitrary array. 您可以使用BitSet
表示一组整数,但不能表示任意数组。 You will lose information about order and repetitions. 您将丢失有关订单和重复的信息。
Basically, set the n
th bit of the BitSet
if and only if n
appears in your set of integers. 基本上,当且仅当n
出现在整数集中时,才设置BitSet
第n
位。
BitSet bitSet = new BitSet();
int[] setOfInts = new int[] { /* Your array here */ };
for (int n : setOfInts) {
bitSet.set(n);
}
first thought: 首先想到:
use BigInteger and create it like: new BigInteger(int value, int base). 使用BigInteger并创建它:new BigInteger(int value,int base)。 Then you can toString() it, and then create BitSet using that String(don't know how to do it without analyzing the string, however). 然后你可以toString()它,然后使用该String创建BitSet(不知道怎么做而不分析字符串)。
-- -
didn't read it right. 没看对。 That method only helps you to create an array of BitSet, not the whole BitSet that contains the whole array. 该方法只能帮助您创建BitSet数组,而不是包含整个数组的整个BitSet。
I don't know how to make array of integers to one bitSet. 我不知道如何将一个整数数组转换为一个bitSet。 I guess you will need some kind of delimeters, but how to make good delimeter in binary - that's a good question. 我想你需要某种分界,但是如何用二进制制作好的分隔符 - 这是一个很好的问题。
我认为逻辑是:运行整数数组,测试每一位并在bitset中设置此位,如bitset.set(array_pos + bit_pos)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.