本文介绍了BitSet的size()方法的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有方法。 html> java.util.BitSet class?

Is there a use case for the size() method on the java.util.BitSet class?

我的意思是 - JavaDoc清楚地说它是依赖于实现,它以位为单位返回内部 long [] 存储的大小。从它所说的,可以得出结论,你将无法设置比 size()更高的索引,但这不是真的, BitSet 可以自动增长:

I mean - the JavaDoc clearly says it's implementation dependant, it returns the size of the internal long[] storage in bits. From what it says, one could conclude that you won't be able to set a bit with a higher index than size(), but that's not true, the BitSet can grow automatically:

BitSet myBitSet = new BitSet();
System.out.println(myBitSet.size());    // prints "64"
myBitSet.set(768);
System.out.println(myBitSet.size());    // prints "832"

每次遇到 BitSet 我一生都有过,我一直想用因为那个返回 BitSet 的逻辑大小:

In every single encounter with BitSet I have had in my life, I always wanted to use length() since that one returns the logical size of the BitSet:

BitSet myBitSet = new BitSet();
System.out.println(myBitSet.length());    // prints "0"
myBitSet.set(768);
System.out.println(myBitSet.length());    // prints "769"

尽管我在过去的6年里一直在编程Java,但是方法总是让我很困惑。我经常混淆它们并偶然使用错误的一个,因为在我的脑海里,我想到 BitSet 作为一个聪明的 Set< boolean> 我在哪里使用 size()

Even though I have been programming Java for the last 6 years, the two methods are always highly confusing for me. I often mix them up and use the wrong one incidentally, because in my head, I think of BitSet as a clever Set<boolean> where I'd use size().

就像 ArrayList length()返回元素数量和 size()返回大小底层数组。

It's like if ArrayList had length() returning the number of elements and size() returning the size of the underlying array.

现在,我缺少 size()方法的用例吗?它有用吗?有没有人用它做任何事情?某些手动钻头或类似的东西可能很重要吗?

Now, is there any use case for the size() method I am missing? Is it useful in any way? Has anyone ever used it for anything? Might it be important for some manual bit twiddling or something similar?

EDIT 更多研究)

我意识到 BitSet 是在Java 1.0中引入的,而Collections框架是我们使用的大多数类在Java 1.2中引入。因此,基本上在我看来, size()由于遗留原因而保留,并且没有真正的用途。新的Collection类没有这样的方法,而有些旧的类(,例如)do。

I realized BitSet was introduced in Java 1.0 while the Collections framework with most of the classes we use was introduced in Java 1.2. So basically it seems to me that size() is kept because of legacy reasons and there's no real use for it. The new Collection classes don't have such methods, while some of the old ones (Vector, for example) do.

推荐答案

正确。

是的,差不多。

另一个size方法是 length(),它为您提供了设置位的最大索引。从逻辑角度来看, length() size()更有用......但是 length()仅在Java 1.2中引入。

The other "size" method is length() which gives you the largest index at which a bit is set. From a logical perspective, length() is more useful than size() ... but length() was only introduced in Java 1.2.

我能想到的唯一(假设的)用例 size()可能优于 length()时间:

The only (hypothetical) use-case I can think of where size() might be better than length() is when:


  • 你正在尝试为集合中的位迭代建立一个fence post,而

  • 你很可能会在之前停止迭代最后,和

  • 没关系,你是否超出了设置的最后一位。

在这种情况下, size()可以说比<$ c更好$ c> length()因为这是一个更便宜的电话。 (看一下源代码......)但这很微不足道了。

In that case, size() is arguably better than length() because it is a cheaper call. (Look at the source code ...) But that's pretty marginal.

(我猜,类似行的另一个用例就是当你创建一个新的 BitSet 并根据现有 BitSet size()预先分配它>。再次,差异是微不足道的。)

(I guess, another use-case along similar lines is when you are creating a new BitSet and preallocating it based on the size() of an existing BitSet. Again, the difference is marginal.)

但你对兼容性是正确的。很明显,他们无法摆脱 size()或更改其语义而不会产生兼容性问题。所以他们可能决定不管它。 (实际上,他们甚至没有看到需要弃用它。在API中使用一种特别有用的方法的危害很小。)

But you are right about compatibility. It is clear that they could not either get rid of size() or change its semantics without creating compatibility problems. So they presumably decided to leave it alone. (Indeed, they didn't even see the need to deprecate it. The "harm" in having a not-particularly-useful method in the API is minimal.)

这篇关于BitSet的size()方法的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:48