轻松学习C#的ArrayList类

2019-12-30 11:09:56于丽
  • {   string[] str = { "a", "b", "c", "d", "d", "e", "f" };  
  • ArrayList al = new ArrayList(str);   int i = al.IndexOf("c");//查找第一个字符c在数组中的位置  
  • Console.WriteLine("元素c在集合中的位置是:"+i);   i = al.LastIndexOf("d");//查找最后一个字符d在数组中的位置  
  • Console.WriteLine("元素d在集合中的位置是:" + i);   int j = al.BinarySearch("f");//查找元素f在数组中的位置  
  • if (j>0)   {  
  • Console.WriteLine("元素f在数组中的位置是:"+j);   }  
  • else  {  
  • Console.WriteLine("没有找到a");   }  
  • Console.ReadLine();   }  
  • }   }</span>  

    输出的结果为:元素c在集合中的位置是:2

    元素d在集合中的位置是:3

    元素f在数组中的位置是:5

    四、ArrayList元素的遍历

    在执行上述程序的过程中已经使用foreach语句进行ArrayList元素的遍历,在这里就不再举例进行说明。

    以上就是关于C#的ArrayList类的相关介绍,希望对大家的学习有所帮助。


    注:相关教程知识阅读请移步到c#教程频道。