首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在VB.Net中创建和循环多维数组?

如何在VB.Net中创建和循环多维数组?
EN

Stack Overflow用户
提问于 2014-10-02 23:25:24
回答 3查看 1.7K关注 0票数 0

我正试图在VB.net中完成以下工作:

代码语言:javascript
运行
复制
' 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

然后,产出应是:

代码语言:javascript
运行
复制
100 Parent1
101 Child1
102 Child2
200 Parent2
201 Child1
300 Parent1
301 Child1
302 Child2
303 Child3

我知道在BV.net中创建的一维数组如下:

代码语言:javascript
运行
复制
Dim Parents As New Dictionary(Of Integer, String) From { 
    {100, "Parent1"}, 
    {200, "Parent2"},
    {300, "Parent3"}
}

但是,我想不出如何把这个扩展到同时容纳孩子们。

问题1:如何更新上面的数组以同时保存每个父项的子级?

每个循环都是用伪代码编写的。

问题2:如何循环多维数组以获得所需的输出?

EN

回答 3

Stack Overflow用户

发布于 2014-10-02 23:44:04

我知道在BV.net中创建的一维数组如下: 从{ {100,"Parent1"},{200,"Parent2"},{300,"Parent3"}

这是一本字典--一个唯一键到值的映射--而不是一维数组。如果您只想迭代它(特别是如果您想这样做的话),那么元组(Tuple(Of Integer, String))或KeyValuePairs的列表可能更合适:

代码语言:javascript
运行
复制
Dim parents As New List(Of Tuple(Of Integer, String)) From {
    Tuple.Create(100, "Parent1"),
    Tuple.Create(200, "Parent2"),
    Tuple.Create(300, "Parent3")
}

您可以将其扩展为一个三项元组,给他们每个孩子:

代码语言:javascript
运行
复制
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") }),
    …
}

然后再迭代一下,

代码语言:javascript
运行
复制
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

不过,这些并不是描述性很强的名称,您可以随时创建自己的类。

代码语言:javascript
运行
复制
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语法:

代码语言:javascript
运行
复制
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

以及:

代码语言:javascript
运行
复制
Dim parents As New List(Of Parent) From {
    New Parent(100, "Parent1") From {
        New Child(101, "Child1"),
        New Child(102, "Child2")
    },
    …
}

我的语法在这些方面可能不太正确,但你明白了。

票数 1
EN

Stack Overflow用户

发布于 2014-10-02 23:45:04

听起来,您需要的不仅仅是一个Dictionary或多维数组:您需要一个类。

父对象至少有三个属性:

代码语言:javascript
运行
复制
 ID
 Name
 Collection of child objects

虽然理论上你可以在一个多维的物体数组中处理所有这些,但是你会把自己绑在绳结上,试图把它保持得笔直。您需要的是一个具有表示这三个属性的属性的类。您的parentchild对象都是该类的实例:child对象只是没有自己的child对象。

如果您不熟悉类的概念,则需要进行一些阅读。了解有关类、属性和集合类的信息。您可能需要特别注意ArrayList类,因为它可能非常有用。(泛型List<T>类甚至更有用,但这是一个更高级的主题。)

票数 0
EN

Stack Overflow用户

发布于 2014-10-03 03:31:09

这不是一个非常有效的解决方案--它将每个家庭成员与其他每个家庭成员进行比较。但是,它可以满足您的需要,并且应该教您一些关于数组如何工作的知识。

代码语言:javascript
运行
复制
  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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26171163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档