vb.net - 多维数组列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3659250/
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
vb.net - multi-dimension array list
提问by Joe
I've managed to make some single dimension array lists but I can't figure out a multi dimension arraylist.
我设法制作了一些单维数组列表,但我无法弄清楚多维数组列表。
Here's what I'm trying to do:
这是我想要做的:
I have a database (mdb) with 5 columns that I want each row to be in an array list.
我有一个包含 5 列的数据库 (mdb),我希望每一行都在一个数组列表中。
In PHP what I'd typically do is:
在 PHP 中,我通常会做的是:
$array[$field1] = array($field2,$field3,$field4,$field5);
$array[$field1] = array($field2,$field3,$field4,$field5);
How I do the same in vb.net so anytime I need to fetch an item for a specific for the row1 I could call it?
我如何在 vb.net 中做同样的事情,所以只要我需要为特定的 row1 获取一个项目,我就可以调用它?
For a single dimension I could do the following, but I can't figure out how to add more fields to a single array row:
对于单个维度,我可以执行以下操作,但我无法弄清楚如何向单个数组行添加更多字段:
Dim tmpArrayX As New ArrayList
tmpArrayX.Add(field(0))
tmpArrayX.Add(field(1))
etc...
回答by Hans Olsson
If you want to use ArrayList
, just make it's items contain other ArrayList
s.
如果您想使用ArrayList
,只需使其项目包含其他ArrayList
s。
Or you could use normal arrays as:
或者您可以将普通数组用作:
Dim multiArray(2, 2) As String
multiArray(0, 0) = "item1InRow1"
multiArray(0, 1) = "item2InRow1"
multiArray(1, 0) = "item1InRow2"
multiArray(1, 1) = "item2InRow2"
Though my personal preference would be to use List
as:
虽然我个人的偏好是List
用作:
Dim multiList As New List(Of List(Of String))
multiList.Add(New List(Of String))
multiList.Add(New List(Of String))
multiList(0).Add("item1InRow1")
multiList(0).Add("item2InRow1")
multiList(1).Add("item1InRow2")
multiList(1).Add("item2InRow2")
Edit: How to find row:
编辑:如何查找行:
Dim listIWant As List(Of String) = Nothing
For Each l As List(Of String) In multiList
If l.Contains("item1InRow2") Then
listIWant = l
Exit For
End If
Next
回答by JoeBob
' This allows adding rows on the fly....Tested and it works!
' 这允许动态添加行......经过测试,它有效!
Dim multiList As New List(Of List(Of String))
Dim ListRow As Integer = 0
For each record in some_source
dim Country as string = record.country 'from some source
dim Date as Date = record.Date 'from some source
dim Venue as string = record.Venue 'from some source
dim Attendance as string = record.Attendance 'from some source
multiList.Add(New List(Of String))
multiList(ListRow).Add(Country)
multiList(ListRow).Add(Date)
multiList(ListRow).Add(Venue)
multiList(ListRow).Add(Rating)
multiList(ListRow).Add(Attendance)
ListRow = ListRow + 1
next