创建无限大小的二维数组? [英] Creating 2-dimensional array with unlimited size?

查看:51
本文介绍了创建无限大小的二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create city distances array CityDistances(CityId1, CityId2) = Distance As 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?

推荐答案

一个可能更好的替代方案(以及更多的 OOP)是通过使用 List(Of Type).

Instead of an array, a possibly better alternative (and a little more OOP) is through the use of a 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.

这篇关于创建无限大小的二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆