C# ArrayList - RemoveRange() 方法

C# ArrayList RemoveRange() 方法用于从 ArrayList 中删除或丢弃一系列元素,从指定的索引开始,最多删除指定数量的元素。

语法

以下是 C# ArrayList RemoveRange() 方法的语法 -

public virtual void RemoveRange (int index, int count);

参数

此方法接受单个参数 -

  • index:要移除元素范围的起始索引,从零开始。
  • count:要移除的元素数量。

返回值

此方法不返回任何值。

示例 1:从 ArrayList 中删除范围中的整数。

以下是 RemoveRange() 方法的基本示例,用于从 ArrayList 中删除范围中的元素 -

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { 1, 2, 2, 3, 4 };
      
      Console.Write("Initial ArrayList: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // 移除总共 2 个元素
      // 从索引 2 开始
      arrayList.RemoveRange(2, 2);
      
      Console.Write( "Updated ArrayList: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

输出

以下是输出 -

Initial ArrayList: 1 2 2 3 4 
Updated ArrayList: 1 2 4 

示例 2:从 ArrayList 中删除一定范围内的字符串

让我们看另一个示例,使用 RemoveRange() 方法从 ArrayList 中删除从起始索引到元素数范围内的字符串 -

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { "Hi", "tutorialspoint", "India", "tutorix", "Hyderabad" };
      
      Console.Write("Initial arrayList: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // 移除总共 3 个元素
      // 从索引 2 开始
      arrayList.RemoveRange(2, 3);
      
      Console.Write( "Updated ArrayList: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

输出

以下是输出 -

Initial arrayList: Hi tutorialspoint India tutorix 
Updated ArrayList: Hi tutorialspoint India 

示例 3:如果范围超出 ArrayList 的边界会怎样?

如果范围超出 ArrayList 的边界,以下示例将抛出"ArgumentException"异常 -

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList {1, 2, 3, 4, 5};
      
      // 移除总共 5 个元素
      // 从索引 2 开始
      arrayList.RemoveRange(2, 5);
      
      Console.Write( "Updated ArrayList: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

输出

以下是输出 -

Unhandled Exception:
System.ArgumentException: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.

示例 4:管理任务列表

在这里,我们创建一个 ArrayList 来存储一些已完成和未完成的任务。我们使用 RemoveRange() 方法从 ArrayList 中丢弃已完成的任务 -

using System;
using System.Collections;

class Task
{
   public string Name { get; set; }
   public bool IsCompleted { get; set; }

   public Task(string name, bool isCompleted)
   {
      Name = name;
      IsCompleted = isCompleted;
   }

   public override string ToString()
   {
      return $"Task: {Name}, Completed: {IsCompleted}";
   }
}

class Program
{
   static void Main()
   {
      // 创建一个 ArrayList 来存储任务
      ArrayList tasks = new ArrayList()
      {
          new Task("Design UI", true),
          new Task("Set up database", true),
          new Task("Write API endpoints", true),
          new Task("Test API endpoints", true),
          new Task("Develop frontend", false),
          new Task("Integrate API with frontend", false),
          new Task("Final testing", false)
      };

      Console.WriteLine("Original Task List:");
      foreach (Task task in tasks)
         Console.WriteLine(task);

      // 删除一系列已完成的任务
      // 从索引 0 到索引 3
      Console.WriteLine("Removing completed tasks (index 0 to 3)...");
      tasks.RemoveRange(0, 4);

      Console.WriteLine("Updated Task List:");
      foreach (Task task in tasks)
         Console.WriteLine(task);
   }
}

输出

以下是输出 -

Original Task List:
Task: Design UI, Completed: True
Task: Set up database, Completed: True
Task: Write API endpoints, Completed: True
Task: Test API endpoints, Completed: True
Task: Develop frontend, Completed: False
Task: Integrate API with frontend, Completed: False
Task: Final testing, Completed: False

Removing completed tasks (index 0 to 3)...

Updated Task List:
Task: Develop frontend, Completed: False
Task: Integrate API with frontend, Completed: False
Task: Final testing, Completed: False

csharp_arraylist.html