Get the index of an element in ArrayList
The ArrayList class implements the IList interface using an array whose
size is dynamically increased as required. It is designed to hold
heterogeneous collections of objects. ArrayList is not guaranteed to be
sorted. The ArrayList capacity is the number of elements it can hold. Its
capacity is automatically increased while adding elements. ArrayList elements
can be accessed by index and it is zero-based. The ArrayList accepts null as a
valid value and also allows duplicate elements.
The following .net c# tutorial code demonstrates how we can get the index of an element in an ArrayList instance. Here we will use the ArrayList class IndexOf() method to get the index of an element in an ArrayList object. We only take the index of the element’s first occurrence within an ArrayList.
The ArrayList IndexOf() method returns the zero-based index of the first occurrence of a value in the ArrayList or in a portion of it. The ArrayList IndexOf(Object) method overload searches for the specified Object and returns the zero-based index of the first occurrence within the entire ArrayList.
The ArrayList IndexOf(object? value) method overload has a parameter named value. The value parameter is the Object to locate in the ArrayList. The value can be null. The method returns the zero-based index of the first occurrence of value within the entire ArrayList if found otherwise it returns -1.
The ArrayList is searched forward starting at the first element and ending at the last element. The ArrayList IndexOf() method performs a linear search. The ArrayList IndexOf() method determines equality by calling Object.Equals.
The following .net c# tutorial code demonstrates how we can get the index of an element in an ArrayList instance. Here we will use the ArrayList class IndexOf() method to get the index of an element in an ArrayList object. We only take the index of the element’s first occurrence within an ArrayList.
The ArrayList IndexOf() method returns the zero-based index of the first occurrence of a value in the ArrayList or in a portion of it. The ArrayList IndexOf(Object) method overload searches for the specified Object and returns the zero-based index of the first occurrence within the entire ArrayList.
The ArrayList IndexOf(object? value) method overload has a parameter named value. The value parameter is the Object to locate in the ArrayList. The value can be null. The method returns the zero-based index of the first occurrence of value within the entire ArrayList if found otherwise it returns -1.
The ArrayList is searched forward starting at the first element and ending at the last element. The ArrayList IndexOf() method performs a linear search. The ArrayList IndexOf() method determines equality by calling Object.Equals.
ArrayListIndexOfMethod.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
ArrayList colors = new ArrayList() {"Olive","HotPink","White","Peru","Pink","Yellow"};
Label1.Text = "ArrayList Elements....";
Label1.Text += "<font color=DodgerBlue>";
foreach (string color in colors)
{
Label1.Text += "<br />" + color;
}
Label1.Text += "</font>";
Label1.Text += "<font color=HotPink>";
Label1.Text += "<br /><br />Index of 'White': " + colors.IndexOf("White");
Label1.Text += "</font>";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to search specified Object and get zero-based index of the first occurrence within the entire ArrayList</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
System.Collections.ArrayList IndexOf() Method
<br /> How to search specified Object and get zero-based
<br /> index of the first occurrence within the entire ArrayList
</h2>
<hr width="550" align="left" color="Navy" />
<br />
<asp:Label
ID="Label1"
runat="server"
ForeColor="Green"
Font-Size="Large"
Font-Names="Courier New"
Font-Italic="true"
Font-Bold="true"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Test ArrayList IndexOf() Method"
Height="45"
Font-Bold="true"
ForeColor="DodgerBlue"
/>
</div>
</form>
</body>
</html>