2014-10-02 375 views
0

我試圖完成在VB.net如下:如何創建和循環遍歷VB.Net中的多維數組?

' Create multidimensional array holding parents and childs 

[100] Parent1 
    [101] Child1 
    [102] Child2 
[200] Parent2 
    [201] Child1 
[300] Parent3 
    [301] Child1 
    [302] Child2 
    [303] Child3 

' Loop through multidimensional array 

For Each Parent in Array 
    Print parent_id parent_name 

    For each Child in Parent 
     Print child_id child_name 
    Next 
Next 

輸出應該然後是:

100 Parent1 
101 Child1 
102 Child2 
200 Parent2 
201 Child1 
300 Parent1 
301 Child1 
302 Child2 
303 Child3 

我知道BV.net一個維數組一樣創建:

Dim Parents As New Dictionary(Of Integer, String) From { 
    {100, "Parent1"}, 
    {200, "Parent2"}, 
    {300, "Parent3"} 
} 

但是,我無法弄清楚如何擴大這個,也舉行孩子。

問題1:我該如何更新上面的數組來保存每個父項的子項?

For Each循環是用僞代碼寫的。

問題2:如何通過多維數組循環獲取我需要的輸出?

+0

你的循環是如何使你失敗的?結果是什麼?是父母和孩子班?代碼在哪裏? – Plutonix 2014-10-02 23:32:15

回答

1

我知道BV.net一個維數組一樣創建:

Dim Parents As New Dictionary(Of Integer, String) From { 
    {100, "Parent1"}, 
    {200, "Parent2"}, 
    {300, "Parent3"} 
} 

這是一本字典 - 獨特的鍵或值的映射 - 而不是一個一維數組。如果你只是想遍歷它(特別是如果你想在爲了做到這一點),元組(Tuple(Of Integer, String))或KeyValuePair s的名單可能是比較合適的:

Dim parents As New List(Of Tuple(Of Integer, String)) From { 
    Tuple.Create(100, "Parent1"), 
    Tuple.Create(200, "Parent2"), 
    Tuple.Create(300, "Parent3") 
} 

您可以擴展成三項元組給他們每個孩子:

Dim parents As New List(Of Tuple(Of Integer, String, List(Of Tuple(Integer, String))) From { 
    Tuple.Create(100, "Parent1", New List(Of Tuple(Integer, String)) From { Tuple.Create(101, "Child1"), Tuple.Create(102, "Child2") }), 
    Tuple.Create(200, "Parent2", New List(Of Tuple(Integer, String)) From { Tuple.Create(201, "Child1") }), 
    … 
} 

並對其進行迭代,

For Each parent In parents 
    Console.WriteLine("[{0}] {1}", parent.Item1, parent.Item2) 

    For Each child In parent.Item3 
     Console.WriteLine(" [{0}] {1}", child.Item1, child.Item2) 
    Next 
Next 

這些都不是很描述性的名字,不過,你可以隨時菊st做你自己的課程。

Class Parent 
    Public Property Number As Integer 
    Public Property Name As String 
    Public Property Children As New List(Of Child)() 

    Public Sub New(number As String, name As String) 
     Me.Number = number 
     Me.Name = name 
    End Sub 
End Class 

Class Child 
    Public Property Number As Integer 
    Public Property Name As String 

    Public Sub New(number As String, name As String) 
     Me.Number = number 
     Me.Name = name 
    End Sub 
End Class 

根據這些如何相關,它可能是適當的從一個人繼承。然後,爲了方便起見,您可以使父項可枚舉;爲更方便,你可以給它一個Add方法,這將使From語法被用作得好:

Class Parent 
    Implements IEnumerable(Of Child) 

    Public Property Number As Integer 
    Public Property Name As String 
    Public Property Children As New List(Of Child)() 

    Public Sub New(number As String, name As String) 
     Me.Number = number 
     Me.Name = name 
    End Sub 

    Public Sub Add(child As Child) 
     Me.Children.Add(child) 
    End Sub 

    Public Function GetEnumerator() As IEnumerator(Of Child) Implements IEnumerable(Of Child).GetEnumerator 
     Return Me.Children.GetEnumerator() 
    End Function 

    Private Function GetEnumerator_() As IEnumerator Implements IEnumerable.GetEnumerator 
     Return Me.GetEnumerator() 
    End Function 
End Class 

和:

Dim parents As New List(Of Parent) From { 
    New Parent(100, "Parent1") From { 
     New Child(101, "Child1"), 
     New Child(102, "Child2") 
    }, 
    … 
} 

我的語法可能不完全正確的,這些,但你明白了。

0

聽起來像你需要的不僅僅是一個Dictionary或多維數組:你需要一個類。

你的父對象至少有三個屬性:

ID 
Name 
Collection of child objects 

雖然理論上你就可以處理所有的對象的多維數組,你會配合自己在海里試圖保持它的所有直。你需要的是一個具有代表這三個屬性的屬性的類。您的parentchild對象都是此類的實例:child對象不會有自己的child對象。

如果你不熟悉班級的概念,你需要做一些閱讀。閱讀關於類,關於屬性和關於集合類的信息。您可能需要特別注意ArrayList課程,因爲它可能非常有幫助。 (通用的List<T>課程將更加有用,但這是一個更高級的主題。)

0

這不是一個非常有效的解決方案 - 它將每個家庭成員與每個家庭成員進行比較。但是,它可以滿足你的需求,並且應該教你一些關於數組如何工作的知識。

Sub ShowFamily() 
    Dim strFamily(-1, -1) As String '0-based (indices start at zero). first index is the person, second index are the properties associated with the person 
    '(*, 0) = Person's name 
    '(*, 1) = Person's ID 
    '(*, 2) = Person's parent's ID 

    '1. Fill array with data 
    Call LoadFamily(strFamily) 
    '2. Go through the array looking for family members that have no parents 
    For i As Integer = 0 To strFamily.GetUpperBound(0) 
     If strFamily(i, 2) = "" Then 
     '3. Found a parent. Output their name 
     Debug.WriteLine(String.Format("{0} {1}", strFamily(i, 1), strFamily(i, 0))) 
     '4. Go through the array a second time, looking for this parent's children 
     For j As Integer = 0 To strFamily.GetUpperBound(0) 
      If strFamily(j, 2) = strFamily(i, 1) Then 'compare child's parent ID with parent's ID - do they match? 
      'found a match - output the child 
      Debug.WriteLine(String.Format(" {0} {1}", strFamily(j, 1), strFamily(j, 0))) 
      End If 
     Next j 
     End If 
    Next i 
    End Sub 

    Sub LoadFamily(ByRef strFamily(,) As String) 
    ReDim strFamily(9, 2) '9 members 
    strFamily(0, 0) = "Parent1" 
    strFamily(0, 1) = "100" 
    strFamily(0, 2) = "" 'no parent 
    ' 
    strFamily(1, 0) = "Parent1Child1" 
    strFamily(1, 1) = "101" 
    strFamily(1, 2) = "100" 'Parent1 
    ' 
    strFamily(2, 0) = "Parent1Child2" 
    strFamily(2, 1) = "102" 
    strFamily(2, 2) = "100" 'Parent1 
    ' 
    strFamily(3, 0) = "Parent2" 
    strFamily(3, 1) = "200" 
    strFamily(3, 2) = "" 'no parent 
    ' 
    strFamily(4, 0) = "Parent2Child1" 
    strFamily(4, 1) = "201" 
    strFamily(4, 2) = "200" 'Parent2 
    ' 
    strFamily(5, 0) = "Parent3" 
    strFamily(5, 1) = "300" 
    strFamily(5, 2) = "" 'no parent 
    ' 
    strFamily(6, 0) = "Parent3Child1" 
    strFamily(6, 1) = "301" 
    strFamily(6, 2) = "300" 'Parent3 
    ' 
    strFamily(7, 0) = "Parent3Child2" 
    strFamily(7, 1) = "302" 
    strFamily(7, 2) = "300" 'Parent3 
    ' 
    strFamily(8, 0) = "Parent3Child3" 
    strFamily(8, 1) = "303" 
    strFamily(8, 2) = "300" 'Parent3 
    'Add a third child to parent 1, to demonstrate that we are actually matching parents to children 
    strFamily(9, 0) = "Parent1Child3" 
    strFamily(9, 1) = "103" 
    strFamily(9, 2) = "100" 'Parent1 
    End Sub