Reputation: 394
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
Upvotes: 3
Views: 1800
Reputation: 394
Well...
I finally did it this way:
List<BitArray> stack = new List<BitArray>(8);
public FPU()
{
//initialise the stack
for (int i = 0; i < stack.Capacity; i++)
{
stack[i] = new BitArray(80);
}
}
Thanks for your answers, which leaded me to this solution, wich seems to work for me.
Have a nice day, and again, thanks!
Upvotes: -1
Reputation: 3183
First create your BitArray array ([]) like this:
BitArray[] stack = new BitArray[8];
and then initialize all seperate bitarrays in a for-loop (something like this):
foreach (BitArray arr in stack)
{
arr = new BitArray(80);
}
Edit: the something like this was more or less a pointer, not actually tested; this below is:
BitArray[] stack = new BitArray[8];
for(int i=0;i<stack.Length;i++)
{
stack[i] = new BitArray(80);
}
stack[0][0] = true;
Upvotes: 2
Reputation: 113402
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);
Upvotes: 4