LastIndexOf(Object) method in ArrayList

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

Introduction

LastIndexOf(Object) method of ArrayList searches for the specified Object and returns the zero-based index of the last occurrence within the entire ArrayList.For example If we want to search any object from the arraylist then the LastIndexOf method searches it from the last occurrence of the arraylist 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.

            String myString = "Hi";

            int myIndex = myArray.LastIndexOf(myString);

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

        }

    }

}


Output

lastindexof1.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.