Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of fixed-length BitArrays

I'm in trouble with a BitArray.

The goal is to simulate a stack of 8 80bit BitArrays, numbered from 0 to 7.

I just need to be able to access them by index, and so I think a simple array will be enough for me.

When initialising a BitArray object, I need to specify the number of bits it will contain, which gives me

BitArray test = new BitArray(80);

How can I do an Array of it, knowing I need to specify the length value?

I've tried several things, like

BitArray[] stack = new BitArray(80)[];

but I always get an error when trying to give it the length...

Any thoughts?

Thanks in advance

like image 956
nrocha Avatar asked Nov 29 '25 08:11

nrocha


1 Answers

Unfortunately, the framework doesn't appear to have a "canonical" array-initialization pattern, as far as I know.

One way, using LINQ, would be:

var stack = Enumerable.Range(0, 8)
                      .Select(i => new BitArray(80))
                      .ToArray();

or:

var stack = Enumerable.Repeat<Func<BitArray>>( () => new BitArray(80), 8)
                      .Select(f => f())
                      .ToArray();

Alternatively,

BitArray[] stack = new BitArray[8];

for(int i = 0; i < stack.Length; i++)
   stack[i] = new BitArray(80);
like image 66
Ani Avatar answered Nov 30 '25 21:11

Ani