2010-09-07 130 views
4

我已经设法做出一些单维数组列表,但我无法弄清楚多维数组列表。vb.net - 多维数组列表

这里就是我想要做的事:

我有一个数据库(MDB)与我想每一行是在一个数组列表,5列。

在PHP什么我通常做的是:

$阵列[$ FIELD1] =阵列($ FIELD2,$字段3,$ field4中,$字段5);

我如何在vb.net中做同样的事情,所以无论何时我需要为特定的行1获取一个项目我可以称之为?

单一维我可以做到以下几点,但我无法弄清楚如何将更多的字段添加到一个单一的阵列排:

Dim tmpArrayX As New ArrayList 
    tmpArrayX.Add(field(0)) 
    tmpArrayX.Add(field(1)) 
    etc... 

回答

16

如果你想使用ArrayList,只要它的项目包含其他ArrayList s。

或者你可以使用正常的数组为:

Dim multiArray(2, 2) As String 
multiArray(0, 0) = "item1InRow1" 
multiArray(0, 1) = "item2InRow1" 
multiArray(1, 0) = "item1InRow2" 
multiArray(1, 1) = "item2InRow2" 

虽然我个人的偏好是使用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") 

编辑:如何找到行:

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 
+1

这有效,但是如何搜索一个项目并返回索引?我说: multiList(0).Add(“item1InRow1”) multiList(0).Add(“item2InRow1”) multiList(1).Add(“item1InRow2”) multiList(1).Add(“item2InRow2 “) 如何搜索”item1InRow2“并返回索引#”1“,以便查询数组中的其他列? – Joe 2010-09-08 09:17:12

+0

@Joe:可以通过它循环,我在上面的答案中添加了一个简单示例。可能会有一些整洁的Linq方式,但我还没有使用太多。 – 2010-09-08 09:51:13

+0

(2,3)列表如何?我无法实现它!你能举个例子吗?提前致谢。 – Behzad 2012-12-15 22:02:35

0

'这允许在飞行中添加行....经测试,它的工作原理!

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