LastIndexOf(Object,Int32) method in ArrayList

In this article I will discuss the LastIndexOf(Object,Int32) method of ArrayList.
  • 1933

Introduction

LastIndexOf(Object,Int32) method of ArrayList searches for the specified object and returns the zero-based index of the last occurrence within the range of elements in the ArrayList that extends from the first element to the specified index..For example If we want to search any object from the arraylist then the LastIndexOf method searches it from the first index position to the index value that we give in this method as a parameter also check the last occurrence within that range  and return the index value of that object.

Example

namespace ConsoleApplication5

{

    class Program

    {

        static void Main(string[] args)

        {

            // Creates a new ArrayList

            ArrayList myArray = new ArrayList();

            myArray.Add("Hi");

            myArray.Add("How");

            myArray.Add("Are");

            myArray.Add("You");

            myArray.Add("Hi");

            myArray.Add("Have");

            myArray.Add("a nice");

            myArray.Add("Day");

            myArray.Add("Hi");

            // Displays the values of the ArrayList.

            Console.WriteLine("The ArrayList contains the following values:");

            foreach (string s in myArray)

            {

                Console.WriteLine(s);

            }

            // Searches for the last occurrence of the value in the first section of the ArrayList.

            String myString = "Hi";

            int myIndex = myArray.LastIndexOf(myString, 6);

            Console.WriteLine("The last occurrence of \"{0}\" between the start and index 6 is at index {1}.", myString, myIndex);

        }

    }

}


Output

 

lastindexof2.jpg
 

Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.