Reversing, and Searching in Arrays using VB.NET
In this article I will explain you about Reversing, and Searching in Arrays using VB.NET.
You can search for the occurrence of a value in an array with the IndexOf and LastIndexOf member functions. IndexOf starts the search from a lower subscript and moves forward, and LastIndexOf starts the search from an upper subscript and moves backwards. Both functions achieve a linear search, visiting each element sequentially until they find the match forward or backward.
The example below illustrates a linear search through an array using the IndexOf and LastIndexOf methods.
Example of Array Linear Search
' Linear Search
Public Class LinearSearcher
Public Shared Sub Main()
Dim myArray As [String]() = New [String](6) {"kama", "dama", "lama", "yama", "pama", "rama", _
"lama"}
Dim myString As [String] = "lama"
Dim myIndex As Int32
' Search for the first occurrence of the duplicated value in a section of the
myIndex = Array.IndexOf(myArray, myString, 0, 6)
Console.WriteLine("The first occurrence of ""{0}"" between index 0 and index 6 is at index {1}.", myString, myIndex)
' Search for the last occurrence of the duplicated value in a section of the
myIndex = Array.LastIndexOf(myArray, myString, 6, 7)
Console.WriteLine("The last occurrence of ""{0}"" between index 0 and index 6 is at index {1}.", myString, myIndex)
Console.ReadLine()
End Sub
End Class
The program in above example, has this output
The String class provides methods for sorting, searching, and reversing that are easy to use. Note that Sort, BinarySearch, and Reverse are all static functions and are used for single-dimensional arrays.
The next example is shows the usage of the Sort, BinarySearch, and Reverse functions.
Example of Array Binarysearch, and Reverse
' Binary Search
' Note an EXCEPTION occurs if the search element is not in the list
' We leave adding this functionality as homework!
Class linSearch
Public Shared Sub Main()
Dim a As Integer() = New Integer(2) {}
Console.WriteLine("Enter number of elements you want to hold in the array (max3)?")
Dim s As String = Console.ReadLine()
Dim x As Integer = Int32.Parse(s)
Console.WriteLine("--------------------------------------------------")
Console.WriteLine(vbLf & " Enter array elements " & vbLf)
Console.WriteLine("--------------------------------------------------")
For i As Integer = 0 To x - 1
Dim s1 As String = Console.ReadLine()
a(i) = Int32.Parse(s1)
Next
Console.WriteLine("Enter Search element" & vbLf)
Console.WriteLine("--------------------------------------------------")
Dim s3 As String = Console.ReadLine()
Dim x2 As Integer = Int32.Parse(s3)
' Sort the values of the Array.
Array.Sort(a)
For i As Integer = 0 To x - 1
Console.WriteLine("--------------Sorted-------------------------")
Console.WriteLine("Element{0} is {1}", i + 1, a(i))
Next
' BinarySearch the values of the Array.
Dim x3 As Integer = Array.BinarySearch(a, DirectCast(x2, [Object]))
Console.WriteLine("--------------------------------------------------")
Console.WriteLine("BinarySearch: " & x3)
Console.WriteLine("Element{0} is {1}", x3, a(x3))
Console.WriteLine("--------------------------------------------------")
' Reverse the values of the Array.
Array.Reverse(a)
Console.WriteLine("-----------Reversed-------------------------------")
For i As Integer = 0 To x - 1
Console.WriteLine("----------------------------------------------")
Console.WriteLine("Element{0} is {1}", i + 1, a(i))
Next
Console.ReadLine()
End Sub
End Class
The program in above example, has this output
Conclusion
Hope this article would have helped you in understanding Sorting, Reversing, and Searching in Arrays in VB.NET. "Using IComparer interface and sort function together" I will describe on next article.