
本站地址:http://www.bajiao123.com

Java.util包简介并教您如何开发应用之二
um1.hasMoreElements())
The hashtable hash1 is:
{
Beijing=Beijing,
Zhejiang=Hangzhou,
Jiangsu=Nanjing}
The size of this hash table is 3
The element of hash is:
Beijing Hangzhou Nanjing
The capatial of Jiangsu is Nanjing
The hashtable hash2 is:
{
Zhejiang=Hangzhou, Jiangsu=Nanjing}
The size of this hash table is 2
//import java.lang.*;
import java.util.BitSet;
public class BitSetApp
{
private static int n=5;
public static void main(String[] args)
{
BitSet set1=new BitSet(n);
for(int i=0;i //将set1的各位赋1,即各位均为true
BitSet set2= new BitSet();
set2=(BitSet)set1.clone();
//set2为set1的拷贝
set1.clear(0);
set2.clear(2);
//将set1的第0位set2的第2位清零
System.out.println
("The set1 is: "+set1);
//直接将set1转换成字符串输出,
输出的内容是set1中值true所处的位置
//打印结果为The set1 is:
{1,2,3,4}
System.out.println
("The hash code of set2 is:
"+set2.hashCode());
//打印set2的hashCode
printbit("set1",set1);
printbit("set2",set2);
//调用打印程序printbit(),
打印对象中的每一个元素
//打印set1的结果为The bit set1 is:
false true true true true
set1.and(set2);
printbit("set1 and set2",set1);
//完成set1 and set2,并打印结果
set1.or(set2);
printbit("set1 or set2",set1);
//完成set1 or set2,并打印结果
set1.xor(set2);
printbit("set1 xor set2",set1);
//完成set1 xor set2,并打印结果
}
//打印BitSet对象中的内容
public static void printbit
(String name,BitSet set)
{
System.out.print
("The bit "+name+" is: ");
for(int i=0;i System.out.print
(set.get(i)+" ");
System.out.println();
}
}
The set1 is: {1, 2, 3, 4}
The hash code of set2 is: 1225
The bit set1 is: false true true true true
The bit set2 is: true true false true true
The bit set1 and set2 is:
false true false true true
The bit set1 or set2 is:
true true false true true
The bit set1 xor set2 is:
false false false false false
public BitSet();
public BitSet(int n);
public void set(int n)
将BitSet对象的第n位设置成1。
public void clear(int n)
将BitSet对象的第n位清零。
public boolean get(int n)
读取位集合对象的第n位的值,它获取的是一个布尔值。当第n位为1时,返回true;第n位为0时,返回false。 public void and(BitSet set)
public void or(BitSet set)
public void xor(BitSet set)
本站地址:http://www.bajiao123.com

