锯齿状阵列尺寸

发布于 2024-09-03 05:45:58 字数 245 浏览 4 评论 0 原文

我正在使用锯齿状数组,并且遇到了一些问题(或者至少我认为我有问题)。这些数组的大小取决于数据库中返回的行数,因此变化很大。我唯一的解决方案是将数组的维度设置得非常大,但这似乎……并不是最好的方法。

int[][] hulkSmash = new int[10][];
hulkSmash[0] = new int[2] {1,2};

如何在不设置维度的情况下使用锯齿状数组,或者这只是 C# 编程的一个事实?

I'm using jagged arrays and have a bit of a problem (or at least I think I do). The size of these arrays are determined by how many rows are returned in a database, so it is very variable. The only solution I have is to set the dimensions of the array to be very huge, but this seems ... just not the best way to do things.

int[][] hulkSmash = new int[10][];
hulkSmash[0] = new int[2] {1,2};

How can I work with jagged arrays without setting the dimensions, or is this just a fact of life with C# programming??

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

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

发布评论

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

评论(4

硬不硬你别怂 2024-09-10 05:45:58

您有 2 个选择:

  • 您可以读取从数据库返回的信息的大小,并在知道要获取的内容后分配数组空间。
  • 否则,您正在寻找的是具有可变维度的数据结构。看一下列表,例如: MSDN: List< T>

You've got 2 choices:

  • You can read the size of the information returned from the database, and allocate your array space at that time, once you know what you're getting.
  • Otherwise, what you're looking for is a data structure that has variable dimensions. Take a look at List for example: MSDN: List< T >
寂寞美少年 2024-09-10 05:45:58

使用 List 和集合的其他示例是继续的最佳方法。我有时发现

List<List<int>> hulkSmash = new List<List<int>>();

语法有点笨拙。幸运的是,有一些选项可以解决这个问题。

我有时使用的第一个选项是创建别名:

using List<List<int>> = HulkSmashCollection;

这可以防止您一直执行嵌套 <<>> 。无论您在何处输入 List>,您都可以输入 HulkSmashCollection (或更短的内容)。


第二个选项是将列表列表包装在名为 MyHulkSmashes 的类中。在这里,您只需提供所需的函数/属性,例如 CountFind() 等。

这两个概念都用于减少屏幕上的代码量,并减少感知的复杂性。

The other examples using List<T> and collections are the best way to proceed. I sometimes find the

List<List<int>> hulkSmash = new List<List<int>>();

syntax a tad unwieldy. Luckily, there are some options to manage this.

The first option I sometimes use is to make an alias:

using List<List<int>> = HulkSmashCollection;

This prevents you from having to do nested <<<Types>>> all the time. Everywhere you'd type List<List<int>> you can instead type HulkSmashCollection (or something shorter still) instead.


The second option is to wrap the List of Lists within a class called, say, MyHulkSmashes. Here, you only provide the functions/properties you need, like Count or Find() etc.

Both these concepts are used to reduce the amount of code on screen, and reduce perceived complexity.

吃→可爱长大的 2024-09-10 05:45:58
List<List<int>> hulkSmash = new List<List<int>>();
hulkSmash.Add(new List<int>() { 1, 2 });

或者

List<int[]> hulkSmash = new List<int[]>();
hulkSmash.Add(new int[] { 1, 2 });
List<List<int>> hulkSmash = new List<List<int>>();
hulkSmash.Add(new List<int>() { 1, 2 });

or

List<int[]> hulkSmash = new List<int[]>();
hulkSmash.Add(new int[] { 1, 2 });
放血 2024-09-10 05:45:58

整数集合(例如 List)将为您提供所需的灵活性,并且不会像分配比您需要的大得多的数组那样浪费空间。

A collection of ints (such as List) will give you the flexibility that you want and not waste space as would allocating an array that is much larger than you will ever need.

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