How to declare, initialize and print three-dimensional (3D) array of integers in VB.NET

1 Answer

0 votes
        Dim arr3D(1, 2, 3) As Integer

        arr3D(0, 0, 0) = 1
        arr3D(0, 0, 1) = 2
        arr3D(0, 0, 2) = 3
        arr3D(0, 0, 3) = 4

        arr3D(0, 1, 0) = 5
        arr3D(0, 1, 1) = 6
        arr3D(0, 1, 2) = 7
        arr3D(0, 1, 3) = 8

        arr3D(0, 2, 0) = 9
        arr3D(0, 2, 1) = 10
        arr3D(0, 2, 2) = 11
        arr3D(0, 2, 3) = 12

        arr3D(1, 0, 0) = 13
        arr3D(1, 0, 1) = 14
        arr3D(1, 0, 2) = 15
        arr3D(1, 0, 3) = 16

        arr3D(1, 1, 0) = 17
        arr3D(1, 1, 1) = 18
        arr3D(1, 1, 2) = 19
        arr3D(1, 1, 3) = 20

        arr3D(1, 2, 0) = 21
        arr3D(1, 2, 1) = 22
        arr3D(1, 2, 2) = 23
        arr3D(1, 2, 3) = 24

        '''' 0,0,3 '''' 0,1,3 '''' 0,2,3
        ''' 0,0,2 '''' 0,1,2 '''' 0,2,2
        '' 0,0,1 '''' 0,1,1 '''' 0,2,1
        ' 0,0,0 '''' 0,1,0 '''' 0,2,0


        '''' 1,0,3 '''' 1,1,3 '''' 1,2,3
        ''' 1,0,2 '''' 1,1,2 '''' 1,2,2
        '' 1,0,1 '''' 1,1,1 '''' 1,2,1
        ' 1,0,0 '''' 1,1,0 '''' 1,2,0

        Dim bound0 As Integer = arr3D.GetUpperBound(0)
        Dim bound1 As Integer = arr3D.GetUpperBound(1)
        Dim bound2 As Integer = arr3D.GetUpperBound(2)

        Console.WriteLine("bound0 = {0}", bound0)
        Console.WriteLine("bound1 = {0}", bound1)
        Console.WriteLine("bound2 = {0}", bound2)
        Console.WriteLine()

        For i As Integer = 0 To bound0
            For j As Integer = 0 To bound1
                For k As Integer = 0 To bound2
                    Console.Write("{0, 3}", arr3D(i, j, k))
                Next
                Console.WriteLine()
            Next
            Console.WriteLine()
        Next

    End Sub

End Module

' run:
' 
' bound0 = 1
' bound1 = 2
' bound2 = 3

' 1  2  3  4
' 5  6  7  8
' 9 10 11 12

' 13 14 15 16
' 17 18 19 20
' 21 22 23 24

 



answered Mar 3, 2016 by avibootz
edited Apr 11, 2016 by avibootz
...