獲取 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

更新時間: 2019 年 12 月 6 日

89 次瀏覽

開啟你的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.