LastIndexOf(Object,Int32,Int32) method in ArrayList

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

Introduction

LastIndexOf(Object,Int32,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 contains the specified number of elements and ends at the specified index..For example If we want to search any object from the arraylist then the LastIndexOf method searches it from the index rage that we specify under the method that means the first Int32 will be the first index value and the second Int32 is the last index value and under this specified indexes the object can be searched and give 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);

            }

            String myString = "Hi";

            // Searches for the last occurrence of the value in a section of the ArrayList.  Note that the start index is greater than the end index because the search is done backward.

            int myIndex = myArray.LastIndexOf(myString, 8, 5);

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

        }

    }

}


Output

lastindex3.jpg 

 

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

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.