C# ArrayList - RemoveAt() 方法

C# ArrayList 的 RemoveAt() 方法用于从 ArrayList 中删除或忽略指定索引位置的元素。

从 ArrayList 中删除元素后,其大小将进行调整,并且 count 属性的值将减一。

语法

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

public virtual void RemoveAtAt (int index);

参数

此方法接受单个参数 -

  • index:要移除元素的索引,从零开始。

返回值

此方法不返回任何值。

示例 1:从 ArrayList 中移除一个整数

以下是 RemoveAt() 方法的基本示例,用于从 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 处的元素
      arrayList.RemoveAt(2);
      
      Console.Write( "Updated ArrayList: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

输出

以下是输出 -

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

示例 2:从 ArrayList 中删除字符串

让我们看另一个示例,其中我们使用 RemoveAt() 方法从 ArrayList 中删除指定索引值处的字符串元素 -

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { "Hi", "tutorialspoint", "India", "tutorix" };
      
      Console.Write("Initial arrayList: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // 删除 3 处的字符串
      arrayList.RemoveAt(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。然后我们使用 RemoveAt() 方法从 ArrayList 中移除指定元素 -

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { "A", 1, 2, 4, "B", "C", "D" };
      
      Console.Write("Initial ArrayList: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // 删除索引 3 处的元素
      arrayList.RemoveAt(3);
      Console.Write( "After First Removed ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
      
      // 删除索引 4 处的元素
      arrayList.RemoveAt(4);
      Console.Write( "After Second Removed: ");
      foreach (var item in arrayList)
      {
         Console.Write(item + " ");
      }
   }
}

输出

以下是输出 -

Initial ArrayList: A 1 2 4 B C D 
After First Removed A 1 2 B C D 
After Second Removed: A 1 2 B D 

示例 4:从 ArrayList 中删除 Person 详细信息

以下示例创建一个 Person 类,将多个 Person 对象存储在 ArrayList 中,并使用 RemoveAt() 方法删除第二个索引处的元素 -

using System;
using System.Collections;
class Person
{
   public string Name { get; set; }
   public int Age { get; set; }

   public Person(string name, int age)
   {
      Name = name;
      Age = age;
   }

   public override string ToString()
   {
      return $"Name: {Name}, Age: {Age}";
   }
}

class Program
{
   static void Main()
   {
      // 创建一个 ArrayList 来存储 Person 对象
      ArrayList people = new ArrayList()
      {
         new Person("Aman", 26),
         new Person("Kumar", 25),
         new Person("Gupta", 24),
         new Person("Akash", 25)
      };

      Console.WriteLine("Original List of People:");
      foreach (Person person in people)
         Console.WriteLine(person);

      // 删除第二个索引处的元素
      Console.WriteLine("Removing the person at index 2 (Gupta)...");
      people.RemoveAt(2);

      Console.WriteLine("Updated List of People:");
      foreach (Person person in people)
         Console.WriteLine(person);
   }
}

输出

以下是输出 -

Original List of People:
Name: Aman, Age: 26
Name: Kumar, Age: 25
Name: Gupta, Age: 24
Name: Akash, Age: 25

Removing the person at index 2 (Gupta)...

Updated List of People:
Name: Aman, Age: 26
Name: Kumar, Age: 25
Name: Akash, Age: 25

csharp_arraylist.html