C# 的动态 2D 结构

发布于 2024-07-13 07:37:29 字数 428 浏览 10 评论 0 原文

我正在开发一个存储类 MyType 的二维数组的类,并希望它使用动态数据类型。 即不是 MyType[,]

MyType[,] 的问题是该类事先不知道数组的大小,而我不想如果在 .NET Framework 的其他地方完成了管理数组大小调整的工作,那么就会遇到麻烦。

该类在任何给定时刻都不知道最大数组大小,但数组将是密集的。 我知道我可以使用静态数组,并根据需要重新分配内存,但如果可能的话,我更愿意使用内置实现。

对于这个目的,有什么比 List> 更好的吗?

编辑1:指定数组是密集的;

编辑 2 和 3:使用 MyType[,] 指定问题

I'm working on a class that is storing a 2D array of class MyType and would like it to use dynamic data types. i.e. not MyType[,]

The problem with MyType[,] is that the class doesn't know the size of the array ahead of time, and I don't want to go to the trouble of managing array re-sizing if it's been done elsewhere in the .NET Frameworks.

The class will not know maximum array size at any given moment, but the array will be dense. I know I can use static arrays, and re-allocate memory as needed, but I'd prefer to use a built-in implementation if possible.

Is there anything better than List<List<MyType>> for this purpose?

Edit 1: specified that array is dense;

Edit 2 and 3: specified problem with MyType[,]

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

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

发布评论

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

评论(3

末が日狂欢 2024-07-20 07:37:33

这取决于您的结构的稀疏程度。 例如,如果您的条目类似于 myTypes[0, 1]myTypes[134, 544],那么您最好使用 稀疏矩阵。 否则,List> 即可。

It depends on how sparse your structure will be. For instance, if your entries will resemble something like myTypes[0, 1] and myTypes[134, 544], you'd be much better off using sparse matrix. Otherwise, List<List<MyType>> will do.

魂牵梦绕锁你心扉 2024-07-20 07:37:33

对于密集的二维矩阵,矩形阵列是理想的。 您在使用 SomeType[,] 时遇到什么问题? 请注意,您可以使用 Array.CreateInstance(type, dim0Size, dim1Size) 或泛型创建动态数组:(

void DoWork<T>(...) {
   T[,] data = ...
}
DoWork<Foo>(...);
DoWork<Bar>(...);

如果您想使用临时的,可以使用 MakeGenericMethod )类型)

For a dense 2D matrix, a rectangular array is ideal. What is the issue you are having with SomeType[,]? Note that you can create dynamic arrays either with Array.CreateInstance(type, dim0Size, dim1Size), or with generics:

void DoWork<T>(...) {
   T[,] data = ...
}
DoWork<Foo>(...);
DoWork<Bar>(...);

(perhaps using MakeGenericMethod if you want to use ad-hoc types)

滥情稳全场 2024-07-20 07:37:32

创建您自己的 List> 封装,例如:

public class Matrix<T>
{
   List<List<T>> matrix;

   public void Add(IEnumerable<T> row)
   {
      List<T> newRow = new List<T>(row);
      matrix.Add(newRow);
   }

   public T this[int x, int y]
   {
      get  { return matrix[y][x]; }
   }
   ....
}

定义您自己的一组操作! 自由吧!

通过封装它,如果还不够,您可以决定稍后采用更优化的实现。

        ICollection<T> rowOne = (ICollection<T>)new List<Int64>();
        rowOne.Add(1);
        rowOneList.Add(2);
        rowOne.Add(3);

        ICollection<T> rowTwo = (ICollection<T>)new List<Int64>();
        rowTwo .Add(4);
        rowTwo .Add(5);
        rowTwo .Add(6);

Create your own List<List<T>> encapsulation like :

public class Matrix<T>
{
   List<List<T>> matrix;

   public void Add(IEnumerable<T> row)
   {
      List<T> newRow = new List<T>(row);
      matrix.Add(newRow);
   }

   public T this[int x, int y]
   {
      get  { return matrix[y][x]; }
   }
   ....
}

define your own set of operation on it ! Be freee !

By encapsulating it, you can decide to go for a more optimised implementation later if it's not sufficient.

        ICollection<T> rowOne = (ICollection<T>)new List<Int64>();
        rowOne.Add(1);
        rowOneList.Add(2);
        rowOne.Add(3);

        ICollection<T> rowTwo = (ICollection<T>)new List<Int64>();
        rowTwo .Add(4);
        rowTwo .Add(5);
        rowTwo .Add(6);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文