Java 中可以声明 1 位变量吗?
我的算法使用一个巨大的布尔数组,正如我所学的那样,每个布尔变量占用 1 个字节。无论如何,有没有声明一个布尔数组并减少内存使用,因为我正在电话环境中工作。
编辑:我和我的朋友正在讨论 BitSet 是否比普通布尔数组慢。请澄清这一点。该算法仍然需要性能作为最佳需求。
My algorithm use a huge array of boolean
, and as I was taught, it take 1 byte for each boolean variable. Is there anyway to declare a boolean array and reduce the memory usage, because I'm working on phone environment.
EDIT: My friend and I are discussing if BitSet is slower than normal Boolean array. Please clarify this. The algorithm still needs performance as best demand.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
BitSet
使用
boolean
与之间的基准测试链接
>位集
BitSet
Link to benchmark between using
boolean
versusBitSet
您也可以使用 EnumSet 。这允许您使用命名位,并且比使用使用索引位的 BitSet 更友好。
例如,
如果合适的话,打印
IMHO EnumSet 会更清晰。
You can use a EnumSet as well. This allows you to use named bits and can be friendlier than using BitSet which uses indexed bits.
e.g.
prints
IMHO EnumSet is much clearer if its appropriate.