Use the IndexOf() and LastIndexOf() methods to display the first and last occurrence : ArrayList « Data Structure « C# / CSharp Tutorial





using System;
using System.Collections;

class MainClass
{
  public static void Main()
  {
    ArrayList myArrayList = new ArrayList();
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    

    string[] anotherStringArray = {"Here's", "some", "more", "text"};
    myArrayList.SetRange(0, anotherStringArray);


    if (myArrayList.Contains("text"))
    {
      int index = myArrayList.IndexOf("text");
      Console.WriteLine("myArrayList does contain the word 'text'");
      Console.WriteLine("'text' first occurs at index " + index);
      index = myArrayList.LastIndexOf("text");
      Console.WriteLine("'text' last occurs at index " + index);
    }

  }
}










11.21.ArrayList
11.21.1.Demonstrates the 3 constructors of ArrayList
11.21.2.Get the number of elements
11.21.3.Remove elements from ArrayList
11.21.4.Capacity and element count
11.21.5.Change contents using array indexing
11.21.6.Sort and search an ArrayList
11.21.7.Use enumerator to access list
11.21.8.Re-enumerate the list
11.21.9.Make an ArrayList Synchronized
11.21.10.Insert and remove at specific index
11.21.11.Clear an ArrayList
11.21.12.Create a string array and use the ICollection.CopyTo method to copy the contents of the ArrayList
11.21.13.Use ArrayList.ToArray to create an object array from the contents of the collection
11.21.14.Box ints into ArrayList
11.21.15.ArrayList: AddRange
11.21.16.Insert item to ArrayList by index
11.21.17.Use ArrayList copy constructor
11.21.18.IsFixedSize and IsReadOnly properties
11.21.19.Insert(), AddRange(), and InsertRange() methods
11.21.20.Use the SetRange() method to copy the elements from anotherStringArray to myArrayList, starting at index 0
11.21.21.Use the Contains() method to determine if the string 'text' is in the ArrayList
11.21.22.Use the IndexOf() and LastIndexOf() methods to display the first and last occurrence
11.21.23.Remove element 0, first 'text' element, and two elements starting at index 3
11.21.24.Use the Reverse() method to reverse myArrayList
11.21.25.Using the TrimToSize() method to reduce the capacity of ArrayList
11.21.26.Using the GetRange() method to get two elements from myArrayList, starting at index 1
11.21.27.Get an enumerator using the GetEnumerator() method and use it to read the elements in ArrayList
11.21.28.Use the Reset() method and access the first row again using MoveNext()
11.21.29.Sort arraylist using custom IComparer
11.21.30.Sort ArrayList using the default comparer and a custom comparer that reverses the sort order.
11.21.31.Filter int from ArrayList
11.21.32.extends ArrayList to create NameCollection
11.21.33.ArrayList Query