⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javautil.doc1.html

📁 java语言规范
💻 HTML
字号:
<html>
<head>
<title>The Java Language Specification The Package java.util</title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
 
<a href="index.html">Contents</a> | <a href="javautil.doc.html">Prev</a> | <a href="javautil.doc2.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
 
<a name="7404"></a>
<center><h1>21.2  The Class  <code>java.util.BitSet</code></h1></center>
<a name="20154"></a>
A <code>BitSet</code> object is a set of bits that grows as needed. The bits of a <code>BitSet</code> are 
indexed by nonnegative integers. Each bit can be individually examined, set, or 
cleared. One <code>BitSet</code> may be used to modify the contents of another <code>BitSet</code> 
through logical AND, logical inclusive OR, and logical exclusive OR operations.
<p><pre><a name="7405"></a>public final class <code><b>BitSet</b></code> implements Cloneable {
<a name="7406"></a>	public <code><b>BitSet</b></code>();
<a name="8460"></a>	public <code><b>BitSet</b></code>(int nbits);
<a name="8468"></a>	public String <code><b>toString</b></code>();
<a name="8461"></a>	public boolean <code><b>equals</b></code>(Object obj) &#32;
<a name="8474"></a>	public int <code><b>hashCode</b></code>();
<a name="8507"></a>	public Object <code><b>clone</b></code>();
<a name="8513"></a>	public boolean <code><b>get</b></code>(int bitIndex);
<a name="8462"></a>	public void <code><b>set</b></code>(int bitIndex);
<a name="7409"></a>	public void <code><b>clear</b></code>(int bitIndex);
<a name="7411"></a>	public void <code><b>and</b></code>(BitSet set);
<a name="7412"></a>	public void <code><b>or</b></code>(BitSet set);
<a name="7413"></a>	public void <code><b>xor</b></code>(BitSet set);
<a name="7415"></a>	public int <code><b>size</b></code>();
<a name="7419"></a>}
</pre><a name="7420"></a>
<p><font size=+1><strong>21.2.1   </strong> <code>public <code><b>BitSet</b></code>()</code></font>
<p>
<a name="20159"></a>
This constructor initializes a newly created <code>BitSet</code> so that all bits are clear.
<p><a name="7421"></a>
<p><font size=+1><strong>21.2.2   </strong> <code>public <code><b>BitSet</b></code>(int nbits)</code></font>
<p>
<a name="20173"></a>
This constructor initializes a newly created <code>BitSet</code> so that all bits are clear. 
Enough space is reserved to explicitly represent bits with indices in the range <code>0</code> 
through <code>nbits-1</code>.
<p><a name="8597"></a>
<p><font size=+1><strong>21.2.3   </strong> <code>public String <code><b>toString</b></code>()</code></font>
<p>
<a name="20182"></a>
For every index for which this <code>BitSet</code> contains a bit in the set state, the decimal 
representation of that index is included in the result. Such indices are listed in 
order from lowest to highest, separated by "<code>, </code>" (a comma and a space) and surrounded
by braces, resulting in the usual mathematical notation for a set of integers.

<p><a name="20226"></a>
Overrides the <code>toString</code> method of <code>Object</code> <a href="javalang.doc1.html#1152">(&#167;20.1.2)</a>.<p>
<a name="20183"></a>
Example:<p>
<pre><a name="20184"></a>BitSet drPepper = new BitSet();
</pre><a name="20185"></a>
Now <code>drPepper.toString()</code> returns <code>"{}"</code>.
<p><pre><a name="23557"></a>drPepper.set(2);
</pre><a name="23558"></a>
Now <code>drPepper.toString()</code> returns <code>"{2}"</code>.
<p><pre><a name="23559"></a>
drPepper.set(4);
<a name="20196"></a>drPepper.set(10);
</pre><a name="20204"></a>
Now <code>drPepper.toString()</code> returns <code>"{2, 4, 10}"</code>.
<p><a name="8599"></a>
<p><font size=+1><strong>21.2.4   </strong> <code>public boolean <code><b>equals</b></code>(Object obj)</code></font>
<p>
<a name="20230"></a>
The result is <code>true</code> if and only if the argument is not <code>null</code> and is a <code>BitSet</code> object 
such that, for every nonnegative <code>int</code> index <code>k</code>:
<p><pre><a name="20251"></a>((BitSet)obj).get(k) == this.get(k)
</pre><a name="20234"></a>
Overrides the <code>equals</code> method of <code>Object</code> <a href="javalang.doc1.html#14865">(&#167;20.1.3)</a>.<p>
<a name="8601"></a>
<p><font size=+1><strong>21.2.5   </strong> <code>public int <code><b>hashCode</b></code>()</code></font>
<p>
<a name="20242"></a>
The hash code depends only on which bits have been set within this <code>BitSet</code>. The 
algorithm used to compute it may be described as follows.
<p><a name="26809"></a>
Suppose the bits in the <code>BitSet</code> were to be stored in an array of <code>long</code> integers called, say, <code>bits</code>, in such a manner that bit <code>k</code> is set in the <code>BitSet</code> (for nonnegative values of <code>k</code>) if and only if the expression:<p>
<pre><a name="26812"></a>
((k&gt;&gt;6) &lt; bits.length) &amp;&amp;
<a name="26825"></a>				((bits[k&gt;&gt;6] &amp; (1L &lt;&lt; (bit &amp; 0x3F))) != 0)
</pre><a name="26813"></a>
is true. Then the following definition of the <code>hashCode</code> method would be a correct 
implementation of the actual algorithm:
<p><pre><a name="26828"></a>
public synchronized int hashCode() {
<a name="26829"></a>	long h = 1234;
<a name="26830"></a>	for (int i = bits.length; --i &gt;= 0; ) {
<a name="26831"></a>		h ^= bits[i] * (i + 1);
<a name="26832"></a>	}
<a name="26833"></a>	return (int)((h &gt;&gt; 32) ^ h);
<a name="26834"></a>}
</pre><a name="26841"></a>
Note that the hash code value changes if the set of bits is altered.
<p><a name="20254"></a>
Overrides the <code>hashCode</code> method of <code>Object</code> <a href="javalang.doc1.html#13784">(&#167;20.1.4)</a>.<p>
<a name="8602"></a>
<p><font size=+1><strong>21.2.6   </strong> <code>public Object <code><b>clone</b></code>()</code></font>
<p>
<a name="20257"></a>
Cloning this <code>BitSet</code> produces a new <code>BitSet</code> that is equal to it.
<p><a name="20263"></a>
Overrides the <code>clone</code> method of <code>Object</code> <a href="javalang.doc1.html#14934">(&#167;20.1.5)</a>.<p>
<a name="8608"></a>
<p><font size=+1><strong>21.2.7   </strong> <code>public boolean <code><b>get</b></code>(int bitIndex)</code></font>
<p>
<a name="20282"></a>
The result is <code>true</code> if the bit with index <code>bitIndex</code> is currently set in this <code>BitSet</code>; 
otherwise, the result is <code>false</code>.
<p><a name="20285"></a>
If <code>bitIndex</code> is negative, an <code>IndexOutOfBoundsException</code> is thrown.<p>
<a name="7422"></a>
<p><font size=+1><strong>21.2.8   </strong> <code>public void <code><b>set</b></code>(int bitIndex)</code></font>
<p>
<a name="20287"></a>
The bit with index <code>bitIndex</code> in this <code>BitSet</code> is changed to the "set" (<code>true</code>) state.
<p><a name="20288"></a>
If <code>bitIndex</code> is negative, an <code>IndexOutOfBoundsException</code> is thrown.<p>
<a name="20344"></a>
If <code>bitIndex</code> is not smaller than the value that would be returned by the <code>size</code> &#32;method <a href="javautil.doc1.html#7429">(&#167;21.2.13)</a>, then the size of this <code>BitSet</code> is increased to be larger than <code>bitIndex</code>.<p>
<a name="7423"></a>
<p><font size=+1><strong>21.2.9   </strong> <code>public void <code><b>clear</b></code>(int bitIndex)</code></font>
<p>
<a name="20299"></a>
The bit with index <code>bitIndex</code> in this <code>BitSet</code> is changed to the "clear" (<code>false</code>) 
state.
<p><a name="20300"></a>
If <code>bitIndex</code> is negative, an <code>IndexOutOfBoundsException</code> is thrown.<p>
<a name="20354"></a>
If <code>bitIndex</code> is not smaller than the value that would be returned by the <code>size</code> method <a href="javautil.doc1.html#7429">(&#167;21.2.13)</a>, then the size of this <code>BitSet</code> is increased to be larger than <code>bitIndex</code>.<p>
<a name="7425"></a>
<p><font size=+1><strong>21.2.10   </strong> <code>public void <code><b>and</b></code>(BitSet set)</code></font>
<p>
<a name="20305"></a>
This <code>BitSet</code> may be modified by clearing some of its bits. For every nonnegative 
<code>int</code> index <code>k</code>, bit <code>k</code> of this <code>BitSet</code> is cleared if bit <code>k</code> of <code>set</code> is clear.
<p><a name="7426"></a>
<p><font size=+1><strong>21.2.11   </strong> <code>public void <code><b>or</b></code>(BitSet set)</code></font>
<p>
<a name="20325"></a>
This <code>BitSet</code> may be modified by setting some of its bits. For every nonnegative 
<code>int</code> index <code>k</code>, bit <code>k</code> of this <code>BitSet</code> is set if bit <code>k</code> of <code>set</code> is set.
<p><a name="7427"></a>
<p><font size=+1><strong>21.2.12   </strong> <code>public void <code><b>xor</b></code>(BitSet set)</code></font>
<p>
<a name="20334"></a>
This <code>BitSet</code> may be modified by inverting some of its bits. For every nonnegative 
<code>int</code> index <code>k</code>, bit <code>k</code> of this <code>BitSet</code> is inverted if bit <code>k</code> of <code>set</code> is set.
<p><a name="7429"></a>
<p><font size=+1><strong>21.2.13   </strong> <code>public int <code><b>size</b></code>()</code></font>
<p>
<a name="24986"></a>
This method returns the number of bits of space actually in use by this <code>BitSet</code> to 
represent bit values.<img src="javautil.doc.anc13.gif"> 
<p>

<hr>
<!-- This inserts footnotes--><p>
<a href="index.html">Contents</a> | <a href="javautil.doc.html">Prev</a> | <a href="javautil.doc2.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<p>
<font size=-1>Java Language Specification (HTML generated by Suzette Pelouch on February 24, 1998)<br>
<i><a href="jcopyright.doc.html">Copyright &#169 1996 Sun Microsystems, Inc.</a>
All rights reserved</i>
<br>
Please send any comments or corrections to <a href="mailto:doug.kramer@sun.com">doug.kramer@sun.com</a>
</font>
</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -