gpt4 book ai didi

java - java中BitSet的nextClearBit()实际上是如何工作的?

转载 作者:行者123 更新时间:2023-11-30 01:58:14 25 4
gpt4 key购买 nike

BitSet 类中的该方法用于返回第一个设置为 false 的位的索引

import java.util.BitSet;
public class BitSetDemo {
public static void main(String[] args) {
BitSet b = new BitSet();
b.set(5);
b.set(9);
b.set(6);
System.out.println(""+b);
System.out.println(b.nextClearBit(5));
System.out.println(b.nextClearBit(9));
}
}
Output :
{5, 6, 9}
7
10

在此代码中,6 在 9 之后设置,但它表明这些值是连续存储的((b.nextClearBit(5) 返回下一个值,即 7)。那么,BitSet 如何存储这些值?

最佳答案

nextClearBit 的 javadoc说:

Returns the index of the first bit that is set to false that occurs on or after the specified starting index.

您已将 5、6 和 9 设置为 true。也就是说,从5开始,第一个设置为false的索引是7。而从9开始,第一个设置为false的索引是10。根据你自己的输出,这也是返回的内容。

如果你想知道如何BitSet工作原理以及它的作用,请阅读其 Javadoc 并查看源代码。它包含在 JDK 中。

关于java - java中BitSet的nextClearBit()实际上是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53694377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com