How to work with one dimension int array in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Public Shared Sub Main(ByVal args As String())
        Dim arr As Integer() = New Integer(9) {}
        Dim arr3, arr4 As Integer()
        Dim arr7 As Integer()
        Dim len As Integer
        Dim rnd As Random = New Random()
        arr4 = New Integer(4) {}
        Console.Write("arr4: ")
        PrintArray(arr4)

        For i As Integer = 0 To arr4.Length - 1
            arr4(i) = rnd.[Next](1, 10)
        Next

        Console.Write("arr4: ")
        PrintArray(arr4)

        For i As Integer = 0 To 10 - 1
            arr(i) = i * i
        Next

        Console.Write("1) arr: ")
        PrintArray(arr)
        ChangeArray(arr)
        Console.Write("2) arr: ")
        PrintArray(arr)

        Dim arr1 As Integer() = New Integer() {1, 2, 3, 4}
        Console.Write("3) arr1: ")
        PrintArray(arr1)

        Dim arr2 As Integer() = {2, 4, 6, 8}
        Console.Write("4) arr2: ")
        PrintArray(arr2)
        arr3 = arr2 ' shallow copy - point at the same location in memory
		' arr3 and arr2 are same array different name
        Console.Write("5) arr3: ")
        PrintArray(arr3)
        arr3(1) = 900 ' arr2[1] will also be 900 (point at the same location in memory)
        Console.Write("6) arr3: ")
        PrintArray(arr3)
        Console.Write("7) arr2: ")
        PrintArray(arr2)

        Dim RetArr As Integer()
        Console.Write("8) RetArr: ")
        RetArr = ReturnArray()
        PrintArray(RetArr)
        arr2(0) = 1200 ' arr3[0] will also be 1200 (point at the same location in memory)
        Console.Write("9) arr2: ")
        PrintArray(arr2)
        Console.Write("10) arr3: ")
        PrintArray(arr3)
        Console.WriteLine("11) arr size: {0}", arr.Length)
        Console.Write("Enter array len: ")

        len = Integer.Parse(Console.ReadLine())
        arr7 = New Integer(len - 1) {} ' dynamic array size
        InitArray(arr7)
        PrintArray(arr7)
    End Sub

    Public Shared Sub InitArray(ByVal arr As Integer())
        Dim rnd As Random = New Random()

        For i As Integer = 0 To arr.Length - 1
            arr(i) = rnd.[Next](1, 10)
        Next
    End Sub

    Public Shared Sub ChangeArray(ByVal arr As Integer())
        For i As Integer = 0 To 10 - 1
            arr(i) = i
        Next
    End Sub

    Public Shared Function ReturnArray() As Integer()
        Dim arr As Integer() = {100, 200, 300}
        Return arr
    End Function

    Public Shared Sub PrintArray(ByVal a As Integer())
        For Each element As Integer In a
            Console.Write(" {0} ", element)
        Next

        Console.WriteLine()
    End Sub
End Class


' run:
'
' arr4:  0  0  0  0  0 
' arr4:  2  2  1  8  3 
' 1) arr:  0  1  4  9  16  25  36  49  64  81 
' 2) arr:  0  1  2  3  4  5  6  7  8  9 
' 3) arr1:  1  2  3  4 
' 4) arr2:  2  4  6  8 
' 5) arr3:  2  4  6  8 
' 6) arr3:  2  900  6  8 
' 7) arr2:  2  900  6  8 
' 8) RetArr:  100  200  300 
' 9) arr2:  1200  900  6  8 
' 10) arr3:  1200  900  6  8 
' 11) arr size: 10
' Enter array len: 4
' 2  8  4  1 
'  

 



answered Mar 4, 2025 by avibootz
edited Mar 4, 2025 by avibootz

Related questions

...