Visual Basic - 将数组中的信息存储到二维数组中

时间:2013-06-07 13:55:22

标签: arrays visual-studio-2008 loops multidimensional-array

我有一些代码,我将一个“地图”存储在一个字符串中然后我将它放入一个数组中。此数组已将字符串分隔为每个索引位置的字符。我现在想把这些字符放到一个二维数组中,如下图所示是我的尝试:

Sub DisplayMap()
    Dim MapTog As String
    MapTog = ("--------------------------------------------------------------------------------") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                          ---------------------                               -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-------------------                                        ---------------------") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                          ---------------------                               -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("-                                                                              -") _
           & ("--------------------------------------------------------------------------------")
    Dim MapSep() As Char = MapTog.ToCharArray
    For Count = 0 To 24
        For Count2 = 0 To 79
            Map(Count)(Count2) = MapSep(Count2)
            Console.Write(MapSep(Count2))
        Next Count2
    Next Count
    Console.SetCursorPosition(0, 0)

End Sub

我认为我的For循环有问题,但我无法理解。非常感谢任何帮助,谢谢! :)

1 个答案:

答案 0 :(得分:0)

这是一种方法:

Module Module1

    Public Map As New List(Of List(Of Char))

    Public Sub Main()
        CreateMap()
        DisplayMap()

        Console.ReadLine()
    End Sub

    Public Sub DisplayMap()
        Console.Clear()
        ' this demonstrates how you can access each char separately:
        For y As Integer = 0 To Map.Count - 1
            For x As Integer = 0 To Map(y).Count - 1
                Console.SetCursorPosition(x, y)
                Console.Write(Map(y)(x))
            Next
        Next

        ' ... or you could do ...

        Console.Clear()
        ' this demonstrates how you can convert each row to a string
        For y As Integer = 0 To Map.Count - 1
            Console.SetCursorPosition(0, y)
            Console.Write(New String(Map(y).ToArray))
        Next
    End Sub

    Public Sub CreateMap()
        Dim MapTog As String = _
             "--------------------------------------------------------------------------------" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                          ---------------------                               -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-------------------                                        ---------------------" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                          ---------------------                               -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "-                                                                              -" & vbCrLf _
           & "--------------------------------------------------------------------------------"

        Map.Clear()
        For Each line As String In MapTog.Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
            Map.Add(New List(Of Char)(line.ToCharArray))
        Next
    End Sub

End Module