Arrays

Miscellaneous Methods

 

VB.NET has introduced some very powerful methods for arrays. We will review some of these functions in this lesson.

 

Array.IndexOf and Array.LastIndexOf

 

Array.IndexOf returns the index of the first occurrence of a value in a one-dimensional array. Array.LastIndexOf returns the index of the last occurrence of a value in a one-dimensional array. These methods provide another way of locating a value in an array.

 

The syntax for each method, respectively, is:

Position = Array.IndexOf(Array, ValueToFind)

and

Position = Array.LastIndexOf(Array, ValueToFind)

where Array is the one-dimensional array to search, ValueToFind is the value that you are looking for, and Position is the index in the array where the item was found. If the item is not found, the value of Position will be -1.

 

Sample program:

 

VB Code:

Screen-shot of run:

Module Module1

 

    Sub Main()

 

        Dim aintNumbers() As Integer = {12, 23, 44, 15, 46, 27, 18, 12}

        Dim intX As Integer

        Dim intPosition As Integer

 

        Console.WriteLine("Array contents:")

        Console.WriteLine()

 

        For intX = 0 To UBound(aintNumbers)

            Console.Write(aintNumbers(intX) & "  ")

        Next

        Console.WriteLine()

        Console.WriteLine()

 

        intPosition = Array.IndexOf(aintNumbers, 12)

        Console.WriteLine("Index of 12: " & intPosition)

        Console.WriteLine()

 

        intPosition = Array.LastIndexOf(aintNumbers, 12)

        Console.WriteLine("Last Index of 12: " & intPosition)

        Console.WriteLine()

 

        Console.WriteLine("Press Enter to close this window.")

        Console.ReadLine()

 

    End Sub

 

End Module

 

You can download the code here.

 

 

Array.Copy

 

The Array.Copy method copies one array (or a section of an array) to another array.  The syntax is:

Array.Copy(SourceArray, DestinationArray, NumberOfItemsToCopy)

- or -

Array.Copy(SourceArray, SourceStartIndex, DestinationArray, DestinationStartIndex, NumberOfItemsToCopy))

 

Sample program:

 

VB Code:

Screen-shot of run:

Module Module1

 

    Sub Main()

 

        Dim aintSource() As Integer = {12, 23, 44, 15, 46, 27, 18, 12}

        Dim aintDest(4)

        Dim intX As Integer

 

        Console.WriteLine("Source Array contents:")

        For intX = 0 To UBound(aintSource)

            Console.Write(aintSource(intX) & "  ")

        Next

        Console.WriteLine()

 

        Array.Copy(aintSource, aintDest, 5)

 

        Console.WriteLine()

        Console.WriteLine("Destination Array contents after copying 5 elements:")

        For intX = 0 To UBound(aintDest)

            Console.Write(aintDest(intX) & "  ")

        Next

        Console.WriteLine()

 

        Console.WriteLine()

        Console.WriteLine("Press Enter to close this window.")

        Console.ReadLine()

 

    End Sub

 

End Module

 

You can download the code here.

 

 

Distinct

 

The Distinct method finds all the unique values in the source array and copies them to the destination array.  The syntax is:

DestinationArray = SourceArray.Distinct.ToArray

 

Sample program:

 

VB Code:

Screen-shot of run:

Module Module1

 

    Private maintRandomNumbers(9) As Integer

    Private maintDisctinctValues() As Integer

 

    Sub Main()

 

        PopulateArray()

 

        maintDisctinctValues = maintRandomNumbers.Distinct.ToArray

 

        Console.WriteLine("The distinct numbers are:")

        For intX = 0 To UBound(maintDisctinctValues)

            Console.Write(CStr(maintDisctinctValues(intX)).PadLeft(5))

        Next

        Console.WriteLine()

        Console.WriteLine()

 

        Console.WriteLine("Press Enter to close this window.")

        Console.ReadLine()

 

    End Sub

 

    Private Sub PopulateArray()

 

        Dim intX As Integer

 

        Console.WriteLine("The numbers generated are:")

 

        Randomize()

        For intX = 0 To 9

            maintRandomNumbers(intX) = GetRandomNumber(1, 10)

            Console.Write(CStr(maintRandomNumbers(intX)).PadLeft(5))

        Next

 

        Console.WriteLine()

        Console.WriteLine()

 

    End Sub

 

    Private Function GetRandomNumber(ByVal pintLowerBound As Integer, _

                                     ByVal pintUpperBound As Integer) _

    As Integer

 

        ' This function will return a random integer that falls within

        ' the range of the two arguments passed.

 

        Return Int((pintUpperBound - pintLowerBound + 1) * Rnd() _

                  + pintLowerBound)

 

    End Function

 

End Module

 

You can download the code here.