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

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,690 questions

55,449 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 190 views
190 views asked Mar 14, 2023 by avibootz
1 answer 239 views
1 answer 134 views
134 views asked Mar 14, 2023 by avibootz
1 answer 232 views
...