获取 C# 中 ArrayList 中元素范围的枚举器
要获取 ArrayList 中元素范围的枚举器,代码如下 -
示例
using System; using System.Collections; public class Demo { public static void Main(){ ArrayList arrList = new ArrayList(); arrList.Add(100); arrList.Add(200); arrList.Add(300); arrList.Add(400); arrList.Add(500); Console.WriteLine("Display elements in a range..."); IEnumerator demoEnum = arrList.GetEnumerator(1, 3); while (demoEnum.MoveNext()) { Object ob = demoEnum.Current; Console.WriteLine(ob); } } }
输出
这将生成以下输出 -
Display elements in a range... 200 300 400
示例
现在让我们看另一个例子 -
using System; using System.Collections; public class Demo { public static void Main(){ ArrayList arrList = new ArrayList(); arrList.Add("Andy"); arrList.Add("Katie"); arrList.Add("Taylor"); arrList.Add("Steve"); arrList.Add("Nathan"); arrList.Add("Carl"); arrList.Add("Mark"); arrList.Add("Amy"); arrList.Add("Jacob"); arrList.Add("John"); Console.WriteLine("Display elements in a range..."); IEnumerator demoEnum = arrList.GetEnumerator(1, 3); while (demoEnum.MoveNext()) { Object ob = demoEnum.Current; Console.WriteLine(ob); } Console.WriteLine("Display elements in a range..."); demoEnum = arrList.GetEnumerator(3, 5); while (demoEnum.MoveNext()) { Object ob = demoEnum.Current; Console.WriteLine(ob); } } }
输出
这将生成以下输出 -
Display elements in a range... Katie Taylor Steve Display elements in a range... Steve Nathan Carl Mark Amy
广告