How to convert nested list to array in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
    Public Shared Sub Main()
        Dim nestedList As List(Of List(Of Integer)) = New List(Of List(Of Integer)) From {
            New List(Of Integer) From {
                1, 2, 3
            },
            New List(Of Integer) From {
                4, 5, 6
            },
            New List(Of Integer) From {
                7, 8, 9
            }
        }
        Dim array As Integer(,) = ConvertNestedListToArray(nestedList)

        For i As Integer = 0 To array.GetLength(0) - 1
            For j As Integer = 0 To array.GetLength(1) - 1
                Console.Write(array(i, j) & " ")
            Next

            Console.WriteLine()
        Next
    End Sub

	Public Shared Function ConvertNestedListToArray(ByVal nestedList As List(Of List(Of Integer))) As Integer(,)
        Dim rows As Integer = nestedList.Count
        Dim cols As Integer = nestedList(0).Count
        Dim array As Integer(,) = New Integer(rows - 1, cols - 1) {}

        For i As Integer = 0 To rows - 1
            For j As Integer = 0 To cols - 1
                array(i, j) = nestedList(i)(j)
            Next
        Next

        Return array
    End Function
End Class

 
' run:
'
' 1 2 3 
' 4 5 6 
' 7 8 9 
'

 



answered Mar 31 by avibootz
...