简体   繁体   English

Visual Basic嵌套数组字符串输出到实际的整数数组吗?

[英]Visual basic nested array string output to an actual array of integers?

Hy, i have to ask you guys a question. 嗨,我不得不问你们一个问题。

I have a function that combines me 10 elements of an array , that generates me a string of all combinations of 5. the base array is an object so it can be char or integer, in this case is integer 0-9. 我有一个函数,可以将array 10个元素组合在一起,可以为我生成5个所有组合的string 。基本数组是一个对象,因此可以是char或integer,在这种情况下为0-9整数。

Dim base() As Object = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}

Output from my function can be this 我的功能的输出可以是这样

""

and is stored in an object variable ( i say object because i can use the same function to output the same result as char use an object char array: 并存储在一个对象变量中(之所以说对象,是因为我可以使用与char使用对象char数组相同的函数来输出相同的结果:

 Dim base() As Object = {"a", "b", "a", "c", "d", "e", "f", "g", "h", "i"} )

After some work if i use the first array: 经过一些工作,如果我使用第一个数组:

<pre lang="cs">Dim base() As Object = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}</pre> 

I have managed to recieve this output : 我设法收到了这个输出:

myarray = "{{0,1,2,3,4},{0,1,2,3,5},{0,1,2,3,6},{0,1,2,3,7},{0,1,2,3,8},{0,1,2,3,9},{0,1,2,4,5},{0,1,2,4,6},{0,1,2,4,7},{0,1,2,4,8},{0,1,2,4,9},{0,1,2,5,6},{0,1,2,5,7},{0,1,2,5,8},{0,1,2,5,9},{0,1,2,6,7},{0,1,2,6,8},{0,1,2,6,9},{0,1,2,7,8},{0,1,2,7,9},{0,1,2,8,9},{0,1,3,4,5},{0,1,3,4,6},{0,1,3,4,7},{0,1,3,4,8},{0,1,3,4,9},{0,1,3,5,6},{0,1,3,5,7},{0,1,3,5,8},{0,1,3,5,9},{0,1,3,6,7},{0,1,3,6,8},{0,1,3,6,9},{0,1,3,7,8},{0,1,3,7,9},{0,1,3,8,9},{0,1,4,5,6},{0,1,4,5,7},{0,1,4,5,8},{0,1,4,5,9},{0,1,4,6,7},{0,1,4,6,8},{0,1,4,6,9},{0,1,4,7,8},{0,1,4,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9},{3,4,5,6,7},{3,4,5,6,8},{3,4,5,6,9},{3,4,5,7,8},{3,4,5,7,9},{3,4,5,8,9},{3,4,6,7,8},{3,4,6,7,9},{3,4,6,8,9},{3,4,7,8,9},{3,5,6,7,8},{3,5,6,7,9},{3,5,6,8,9},{3,5,7,8,9},{3,6,7,8,9},{4,5,6,7,8},{4,5,6,7,9},{4,5,6,8,9},{4,5,7,8,9},{4,6,7,8,9},{5,6,7,8,9}} " 

that is stored in an object variable. 存储在object变量中。

My question is how to transform the output above(the object variable) in an actual multi dimensional array ? 我的问题是如何在实际的dimensional array转换上面的输出(对象变量)?

Ex: Dim convertedarray() = convert(myarray) 例如: Dim convertedarray() = convert(myarray)

My second question is how to calculate all arrays from above(myarray) and store the results in in another multi dimensional array for ex: dim myresults = calculate(myarray) to output something like this: myresults = {{10},{11},{etc....}} this is only when i use integer arrays, i dont want to calculate chars one... 我的第二个问题是如何从上述(myarray)计算所有arrays并将结果存储在另一个multi dimensional array ,例如: dim myresults = calculate(myarray)以输出如下内容: myresults = {{10},{11},{etc....}}这仅在我使用整数数组时,我不想计算一个char ...

I've written up a quick example of converting a string to a 2D array, if you know regular expressions you could simplify it a bit more. 我已经写了一个将字符串转换为2D数组的简单示例,如果您知道正则表达式,则可以进一步简化它。

But I think you'd find it a lot easier to build it up as you find your combinations. 但我认为,找到组合后,构建起来会容易得多。

Also, using lists might be more straightforward, you wont need to calculate a dimension size. 同样,使用列表可能更简单,您将不需要计算尺寸大小。

Sub Main()

    Dim Arraystring As String = "{{1,2,3,4,5},{2,3,4,5,1},{3,4,5,1,2}}"


    Dim comboTotal As Integer = 3
    Dim comboSize As Integer = 5
    'Need to know dimension sizes to create 2d array - otherwise use list
    Dim DimensionalArray(comboTotal, comboSize) As Integer


    Console.WriteLine("Initial String: " & Arraystring & vbNewLine)


    'Clean up string into CSV
    Dim subStringList() As String = Arraystring.Split("},")


    For i = 0 To subStringList.Count - 1
        subStringList(i) = subStringList(i).Replace(",{", "")
        subStringList(i) = subStringList(i).Replace("{", "")
        Console.WriteLine(subStringList(i))
    Next

    'Start adding and moving down the array
    For i = 0 To subStringList.Count - 1

        'Count array index
        Dim numCount = 0

        'Read in each number - implictly converted to int
        For Each number In subStringList(i).Split(New String() {","}, StringSplitOptions.RemoveEmptyEntries)
            Console.WriteLine("Adding Value: " & number)
            DimensionalArray(i, numCount) = number

            'Move along the array
            numCount += 1
        Next
    Next



    'Try accessing some elements of the new array
    Console.WriteLine(vbNewLine & "Accessing [0][4]: " & DimensionalArray(0, 4))
    Console.WriteLine("Accessing [1][2]: " & DimensionalArray(1, 2))
    Console.WriteLine("Accessing [2][0]: " & DimensionalArray(2, 0))


    Console.ReadLine()

End Sub

控制台输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM