vb.net 创建无限大小的二维数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13102579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 10:55:54  来源:igfitidea点击:

Creating 2-dimensional array with unlimited size?

vb.netmultidimensional-array

提问by barakuda28

I am trying to create city distances array CityDistances(CityId1, CityId2) = DistanceAs I don't know how many citys there would be, I need an unlimited array. I tried creating it via Dim CityDistances(,) As Double, but when I try to use it, it throws an exception. How do I achieve that?

我正在尝试创建城市距离数组CityDistances(CityId1, CityId2) = Distance因为我不知道会有多少个城市,所以我需要一个无限的数组。我尝试通过创建它Dim CityDistances(,) As Double,但是当我尝试使用它时,它会引发异常。我如何做到这一点?

回答by Steve

Instead of an array, a possibly better alternative (and a little more OOP) is through the use of a List(Of Type).

代替数组,一个可能更好的替代方案(以及更多的面向对象编程)是通过使用List(Of Type).

Dim d As List(Of DIstances) = New List(Of Distances)()
d.Add(New Distances() With {.CityID1=1, .CityID2=2, .Distance=12.4})
d.Add(New Distances() With {.CityID1=1, .CityID2=3, .Distance=15.1})
d.Add(New Distances() With {.CityID1=1, .CityID2=4, .Distance=2.4})
d.Add(New Distances() With {.CityID1=1, .CityID2=5, .Distance=130.1})

Public Class Distances
    Public CityID1 as Integer
    Public CityID2 as Integer
    Public Distance as Double
End Class

This has the advantage to let your list grow without specifying any initial limits.

这有利于让您的列表增长而无需指定任何初始限制。

回答by Neolisk

First of all, by doing this:

首先,通过这样做:

Dim CityDistances(,) As Double

You declare a pointer to a two dimensional array, without performing any memory allocation for its elements. It is expected that some method will return this array, and you will use it via this pointer. If you try to use it AS IS, without assigning anything to it, you will get index-out-of-boundsexception, which is normal.

您声明一个指向二维数组的指针,而不对其元素执行任何内存分配。预计某些方法将返回此数组,您将通过此指针使用它。如果您尝试按原样使用它,而不为其分配任何内容,则会出现index-out-of-bounds异常,这是正常的。

Second, there is no such thing as unlimited arrays. You need to be using list of lists, dictionary of dictionaries, a DataTableor similar, if you want automatic memory management. If you want to stick with arrays, for performance/convenience reasons, you can ReDimit when required (increase dimensions), and preserve contents, like this:

其次,没有无限数组这样的东西。DataTable如果您想要自动内存管理,您需要使用列表列表、字典字典、a或类似内容。如果您想坚持使用数组,出于性能/方便的原因,您可以ReDim在需要时使用它(增加维度),并保留内容,如下所示:

Dim CityDistances(,) As Double
ReDim Preserve CityDistances(10,10)
'.... code goes here ....
ReDim Preserve CityDistances(100,100)

Make sure you know when to ReDim, because every time you do it, .NET will create another array and copy all elements there. As the size of your array grows, it may become a performance factor.

确保您知道何时进行 ReDim,因为每次这样做时,.NET 都会创建另一个数组并将所有元素复制到那里。随着阵列大小的增长,它可能会成为一个性能因素。

Third, based on the nature of your question, you may want to look into custom implementations of the Matrixclass. Here are some links I found through Google, hope you find them useful. Those are C#, but there are free online converters to VB.NETon the internet.

第三,根据您的问题的性质,您可能需要查看Matrix该类的自定义实现。以下是我通过 Google 找到的一些链接,希望您觉得它们有用。那些是 C#,但互联网上有免费的 VB.NET 在线转换器

Lightweight fast matrix class in C# (Strassen algorithm, LU decomposition)(free)

C#中的轻量级快速矩阵类(Strassen算法,LU分解)(免费)

Efficient Matrix Programming in C#(code-project, so free as well)

C# 中的高效矩阵编程(代码项目,也是免费的)

High performance matrix algebra for .NET programming!(paid, 99$ and up)

用于 .NET 编程的高性能矩阵代数!(付费,99 美元及以上)