Mega Code Archive

 
Categories / VB.Net / Data Structure
 

ArrayList LastIndexOf searches for Object and returns the zero-based index of the last occurrence

Imports System Imports System.Collections Imports Microsoft.VisualBasic Public Class SamplesArrayList    Public Shared Sub Main()       Dim myAL As New ArrayList()       myAL.Add("A")       myAL.Add("B")       myAL.Add("C")       myAL.Add("D")       myAL.Add("E")       myAL.Add("C")       myAL.Add("D")       myAL.Add("E")       myAL.Add("C")       myAL.Add("D")       myAL.Add("E")       PrintIndexAndValues(myAL)       Dim myString As [String] = "C"       Dim myIndex As Integer = myAL.LastIndexOf(myString)       Console.WriteLine("The last occurrence of ""{0}"" is at index {1}.", myString, myIndex)       myIndex = myAL.LastIndexOf(myString, 8)       Console.WriteLine("The last occurrence of ""{0}"" between the start and index 8 is at index {1}.", myString, myIndex)       myIndex = myAL.LastIndexOf(myString, 10, 6)       Console.WriteLine("The last occurrence of ""{0}"" between index 10 and index 5 is at index {1}.", myString, myIndex)    End Sub    Public Shared Sub PrintIndexAndValues(myList As IEnumerable)       Dim i as Integer       Dim obj As [Object]       For Each obj In  myList          Console.WriteLine("   [{0}]:    {1}", i, obj)          i = i + 1       Next obj       Console.WriteLine()    End Sub End Class