在 C# 中将集合中的元素复制到 ArrayList 中的元素范围内

csharpserver side programmingprogramming更新于 2025/10/7 4:25:00

要在 ArrayList 中将集合中的元素复制到元素范围内,代码如下 −

示例

using System;
using System.Collections;
public class Demo {
   public static void Main(){
      ArrayList arrList = new ArrayList();
      arrList.Add("A");
      arrList.Add("B");
      arrList.Add("C");
      arrList.Add("D");
      Console.WriteLine("ArrayList elements...");
      for (int i = 0; i < arrList.Count; i++) {
         Console.WriteLine("" + arrList[i]);
      }
      string[] str = { "Demo", "Text" };
      arrList.SetRange(0, str);
      Console.WriteLine("After copying...");
      for (int i = 0; i < arrList.Count; i++) {
         Console.WriteLine("" + arrList[i]);
      }
   }
}

输出

这将产生以下输出 −

ArrayList elements...
A
B
C
D
After copying...
Demo
Text
C
D

示例

现在让我们看另一个例子 −

using System;
using System.Collections;
public class Demo {
   public static void Main(){
      ArrayList arrList = new ArrayList();
      arrList.Add("One");
      arrList.Add("Two");
      arrList.Add("Three");
      arrList.Add("Four");
      arrList.Add("Five");
      arrList.Add("Six");
      arrList.Add("Seven");
      arrList.Add("Eight");
      Console.WriteLine("ArrayList elements...");
      for (int i = 0; i < arrList.Count; i++) {
         Console.WriteLine("" + arrList[i]);
      }
      string[] str = { "Demo", "Text" };
      arrList.SetRange(2, str);
      Console.WriteLine("After copying...");
      for (int i = 0; i < arrList.Count; i++) {
         Console.WriteLine("" + arrList[i]);
      }
   }
}

输出

这将产生以下输出 −

ArrayList elements...
One
Two
Three
Four
Five
Six
Seven
Eight
After copying...
One
Two
Demo
Text
Five
Six
Seven
Eight

相关文章