阅读量:3
是的,C#中的ArrayList类可以进行元素查找。你可以使用IndexOf方法来查找特定元素在ArrayList中的索引位置。以下是一个示例:
using System;
using System.Collections.ArrayList;
class Program
{
static void Main()
{
ArrayList myList = new ArrayList();
myList.Add("Apple");
myList.Add("Banana");
myList.Add("Cherry");
string itemToFind = "Banana";
int index = myList.IndexOf(itemToFind);
if (index != -1)
{
Console.WriteLine($"Element '{itemToFind}' found at index {index}.");
}
else
{
Console.WriteLine($"Element '{itemToFind}' not found.");
}
}
}
在这个示例中,我们创建了一个包含三个元素的ArrayList,然后使用IndexOf方法查找字符串"Banana"的索引位置。如果找到了该元素,IndexOf方法将返回其索引值,否则返回-1。
以上就是关于“c# arraylist能进行查找元素吗”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm