Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,923 questions

51,856 answers

573 users

How to add a column to 2D array in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
	Public Shared Function AddColumn(ByVal original As Integer(,), ByVal new_col As Integer()) As Integer(,)
        Dim lastRow As Integer = original.GetUpperBound(0)
        Dim lastColumn As Integer = original.GetUpperBound(1)

		Dim new_arr2d As Integer(,) = New Integer(lastRow + 1 - 1, lastColumn + 2 - 1) {}

        For i As Integer = 0 To lastRow
            For j As Integer = 0 To lastColumn
                new_arr2d(i, j) = original(i, j)
            Next
        Next

        For i As Integer = 0 To new_col.Length - 1
            new_arr2d(i, lastColumn + 1) = new_col(i)
        Next

        Return new_arr2d
    End Function

    Public Shared Sub PrintArray(ByVal array As Integer(,))
        For i As Integer = 0 To array.GetUpperBound(0)
            For j As Integer = 0 To array.GetUpperBound(1)
                Console.Write(array(i, j) & " ")
            Next
            Console.WriteLine()
        Next
    End Sub

    Public Shared Sub Main()
        Dim arr2d As Integer(,) = {
        {1, 2, 3},
        {3, 4, 6}}
        arr2d = AddColumn(arr2d, New Integer() {7, 8})
        PrintArray(arr2d)
    End Sub
End Class




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

 



answered Mar 14, 2023 by avibootz

Related questions

1 answer 123 views
123 views asked Mar 14, 2023 by avibootz
1 answer 185 views
1 answer 85 views
85 views asked Mar 14, 2023 by avibootz
1 answer 174 views
...