I’m trying to Populate an array of BitSets with information about an int array
//ar is a SIZE*SIZE 1-D int array, SIZE is a constant
//decare BitSet array
BitSet bs[] = new BitSet[SIZE];
//initialize BitSet array
for (BitSet x:bs)
x = new BitSet();
//populate BitSet array
for (int i = 0 ; i < ar.length ; i++)
if (ar[i] > 0)
bs[(i/SIZE)].set(SIZE-(i%SIZE));
The last line is what seems to be the problem but I can’t see anything wrong with the logic. Any ideas?
To make this a bit more clear, if I had an array like
0,3,4,5,0,1,0,0,1,2,7,0,2,3,0,3,2,0,0,0,2,1,5,8,0,0,0,0,0,1,0,0,0,6,0,0
I want to get a bitset array that could be represented as
011101,001110,110110,001111,000001,000100
You’re problem is a miss understanding of the enhanced for loop. Basically:
Is just a piece of syntatic sugar. The compiler expands it to the following:
In this expansion it’s quite clear that you’re simply assigning the new bit set to
xand throwing it away. You’re not actually modifying the array at all. You need to initialize the array as follows: