How to print 2D array in VB.NET

4 Answers

0 votes
Imports System

Public Module Program
    Public Sub Main()
        Dim arr2d As Integer(,) = New Integer(,) {
        	{0, 1, 2, 3},
        	{4, 5, 6, 7},
        	{8, 9, 10, 11}}
		
        Dim rows As Integer = arr2d.GetLength(0)
        Dim cols As Integer = arr2d.GetLength(1)

        For i As Integer = 0 To rows - 1
            For j As Integer = 0 To cols - 1
                Console.Write("{0} ", arr2d(i, j))
            Next
            Console.WriteLine()
        Next
    End Sub
End Module




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

 



answered Mar 12, 2023 by avibootz
0 votes
Imports System

Public Module Program
    Public Sub Main()
        Dim arr2d As Integer(,) = New Integer(,) {
        	{0, 1, 2, 3},
        	{4, 5, 6, 7},
        	{8, 9, 10, 11}}
		
        For Each num As Integer In arr2d
            Console.Write("{0} ", num)
        Next
    End Sub
End Module




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

 



answered Mar 12, 2023 by avibootz
0 votes
Imports System
Imports System.Linq

Public Module Program
    Public Sub Main()
        Dim arr2d As Integer(,) = New Integer(,) {
        	{0, 1, 2, 3},
        	{4, 5, 6, 7},
        	{8, 9, 10, 11}}
		
        Console.WriteLine(String.Join(" ", arr2d.Cast(Of Integer)()))
    End Sub
End Module




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

 



answered Mar 12, 2023 by avibootz
0 votes
Imports System
Imports System.Linq
Imports System.Collections.Generic

Public Module Program
    Public Sub Main()
        Dim arr2d As Integer(,) = New Integer(,) {
        	{0, 1, 2, 3},
        	{4, 5, 6, 7},
        	{8, 9, 10, 11}}
		
        Dim lst As List(Of Integer) = arr2d.Cast(Of Integer)().ToList()
		
        lst.ForEach(AddressOf Console.WriteLine)
    End Sub
End Module




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

 



answered Mar 12, 2023 by avibootz
...