I am a beginner programmer and I recently came across a data structure BitArray while reading one book (Programming Pearls, to be more precise). I wanted to learn and practice a bit on BitArray as I had no previous knowledge of it. I did a simple implementation in C# (easy things like creating the BitArray, set values, and, or etc), but when I wanted to try in Java, it complained that BitArray is not known. So I searched online and changed BitArray to BitSet. Is BitSet the equivalent of BitArray in Java? Also, I don't quite understand the different meanings of size() and length() in BitSet. Check the code below:
public class Sandbox {
public static void main(String argv[])
{
BitSet bitSet1 = new BitSet(16);
bitSet1.set(0);
bitSet1.set(8);
bitSet1.set(15);
displayBitSet(bitSet1);
}
static void displayBitSet(BitSet bitSet)
{
for(int i=0;i<bitSet.size();i++)
{
boolean bit = bitSet.get(i);
System.out.print(bit?1:0);
}
System.out.println(" "+bitSet.size()+" "+bitSet.length());
}
}
Output is:
1000000010000001000000000000000000000000000000000000000000000000 64 16
I thought I would get something like
1000000010000001 16 16
Where do these trailing zeroes come from? Can someone explain this to me? thanks~~