Redian新闻
>
请问一个有关选择数据结构的问题
avatar
请问一个有关选择数据结构的问题# Java - 爪哇娇娃
r*t
1
在java程序中需要用到一个布尔数组,它的大小是可变的(从0开始,可能会增加到
1K到1M)。我的要求是尽可能地节省空间。我可以想到这些办法:
直接用数组。但是数组一旦初始化,就不可以改变长度,这种情况下我很难选择初
始化的数组大小。
初始化一定大小的布尔数组,用满了以后,产生一个更大的新数组,再把原来的数
据copy进去。我不知道这种情况下,java的内存管理机制会不会完全释放原来的数
组?如果内存资源不能完全回收,出来的performance会有问题。
用java已有的类,比如vector或者arraylist。以前没有用过这两个,不知道哪个更
好。也不知道有没有更合适的。
请各位大侠指点!!
多谢多谢!!!!
avatar
o*e
2

then look again at java.util package.

【在 r**t 的大作中提到】
: 在java程序中需要用到一个布尔数组,它的大小是可变的(从0开始,可能会增加到
: 1K到1M)。我的要求是尽可能地节省空间。我可以想到这些办法:
: 直接用数组。但是数组一旦初始化,就不可以改变长度,这种情况下我很难选择初
: 始化的数组大小。
: 初始化一定大小的布尔数组,用满了以后,产生一个更大的新数组,再把原来的数
: 据copy进去。我不知道这种情况下,java的内存管理机制会不会完全释放原来的数
: 组?如果内存资源不能完全回收,出来的performance会有问题。
: 用java已有的类,比如vector或者arraylist。以前没有用过这两个,不知道哪个更
: 好。也不知道有没有更合适的。
: 请各位大侠指点!!

avatar
g*g
3
check BitSet

【在 o***e 的大作中提到】
:
: then look again at java.util package.

avatar
m*t
4

I'd go with ArrayList unless your project is on J2ME.
Just remember to use Boolean.valueOf(value) rather than new Boolean(value)
everytime when you insert an element. That way you won't end up with
a bunch of different Boolean objects.

【在 r**t 的大作中提到】
: 在java程序中需要用到一个布尔数组,它的大小是可变的(从0开始,可能会增加到
: 1K到1M)。我的要求是尽可能地节省空间。我可以想到这些办法:
: 直接用数组。但是数组一旦初始化,就不可以改变长度,这种情况下我很难选择初
: 始化的数组大小。
: 初始化一定大小的布尔数组,用满了以后,产生一个更大的新数组,再把原来的数
: 据copy进去。我不知道这种情况下,java的内存管理机制会不会完全释放原来的数
: 组?如果内存资源不能完全回收,出来的performance会有问题。
: 用java已有的类,比如vector或者arraylist。以前没有用过这两个,不知道哪个更
: 好。也不知道有没有更合适的。
: 请各位大侠指点!!

avatar
m*t
5

Good idea. The only caveat though is that it doesn't implement
any collection interface, which may or may not matter depending on
how big of an abstraction freak you are. 8-)
BTW, don't you find it strange that it's called BitSet while
it's essentially a sequence of bits?



【在 g*****g 的大作中提到】
: check BitSet
avatar
g*g
6
It's performance issue that holded the designers back from implementing any
collection interface.
Boolean value is stored as a bit in integer and it will be very expensive to
implement method like remove(int index).
The BitSet is only good for store large array of boolean values with trading
performance for space in mind.







【在 m******t 的大作中提到】
:
: Good idea. The only caveat though is that it doesn't implement
: any collection interface, which may or may not matter depending on
: how big of an abstraction freak you are. 8-)
: BTW, don't you find it strange that it's called BitSet while
: it's essentially a sequence of bits?
: 到
: 更

avatar
m*t
7

I understand. That's why I said "depending on how big of an abstraction
freak you are." 8-)
Although one might argue that performance issues should only hold back
the *usage* of an API, rather than the *implementation* of it. Removing
a bit is expensive doesn't mean it can't/shouldn't be implemented. As
long as it's clearly documented, the user should be allowed to make a
conscious decision to still use the API.
E.g., if a BitSet occasionally
needs to be passed to an API taking Collection inte

【在 g*****g 的大作中提到】
: It's performance issue that holded the designers back from implementing any
: collection interface.
: Boolean value is stored as a bit in integer and it will be very expensive to
: implement method like remove(int index).
: The BitSet is only good for store large array of boolean values with trading
: performance for space in mind.
:
: 加
: 初
: 数

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。