简体   繁体   中英

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.

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:

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

that is stored in an object variable.

My question is how to transform the output above(the object variable) in an actual multi dimensional array ?

Ex: 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...

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.

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

控制台输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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