固定长度 BitArray 数组

发布于 2024-10-05 12:53:26 字数 426 浏览 0 评论 0原文

我遇到了 BitArray 的麻烦。

目标是模拟 8 个 80 位 BitArray 的堆栈,编号从 0 到 7。

我只需要能够通过索引访问它们,所以我认为一个简单的数组对我来说就足够了。

初始化 BitArray 对象时,我需要指定它将包含的位数,这给了我

BitArray test = new BitArray(80);

如何在知道我需要指定长度值的情况下创建它的数组?

我已经尝试了几件事,例如

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

,但在尝试给出长度时我总是遇到错误......

有什么想法吗?

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄情伤 2024-10-12 12:53:27

不幸的是,据我所知,该框架似乎没有“规范的”数组初始化模式。

使用 LINQ 的一种方法是:

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

或:

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

或者,

BitArray[] stack = new BitArray[8];

for(int i = 0; i < stack.Length; i++)
   stack[i] = new BitArray(80);

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);
丑丑阿 2024-10-12 12:53:27

首先像这样创建 BitArray 数组 ([]):

BitArray[] stack = new BitArray[8];

然后在 for 循环中初始化所有单独的位数组(类似这样):

foreach (BitArray arr in stack)
{
    arr = new BitArray(80);
}

编辑: 这样的东西或多或少是一个指针,而不是实际测试过;下面是:

BitArray[] stack = new BitArray[8];
for(int i=0;i<stack.Length;i++)
{
    stack[i] = new BitArray(80);
}
stack[0][0] = true;

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;
不甘平庸 2024-10-12 12:53:27

好吧...

我终于这样做了:

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);
    }
}

感谢您的回答,这使我找到了这个解决方案,这似乎对我有用。

祝您有美好的一天,再次感谢!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文