我正在开发一个存储类 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[,]
发布评论
评论(3)
这取决于您的结构的稀疏程度。 例如,如果您的条目类似于
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]
andmyTypes[134, 544]
, you'd be much better off using sparse matrix. Otherwise,List<List<MyType>>
will do.对于密集的二维矩阵,矩形阵列是理想的。 您在使用
SomeType[,]
时遇到什么问题? 请注意,您可以使用 Array.CreateInstance(type, dim0Size, dim1Size) 或泛型创建动态数组:(如果您想使用临时的,可以使用 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 withArray.CreateInstance(type, dim0Size, dim1Size)
, or with generics:(perhaps using
MakeGenericMethod
if you want to use ad-hoc types)创建您自己的
List
封装,例如:>
定义您自己的一组操作! 自由吧!
通过封装它,如果还不够,您可以决定稍后采用更优化的实现。
Create your own
List<List<T>>
encapsulation like :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.