📌  相关文章
📜  C#|如何从列表的指定索引中删除元素

📅  最后修改于: 2021-05-29 16:27:05             🧑  作者: Mango

List .RemoveAt(Int32)方法用于删除List 的指定索引处的元素。

列表的属性:

  • 它与数组不同。列表可以动态调整大小,但数组则不能。
  • 列表类可以接受null作为引用类型的有效值,并且还允许重复的元素。
  • 如果计数等于容量,则列表的容量将通过重新分配内部数组而自动增加。在添加新元素之前,现有元素将被复制到新数组。

句法:

public void RemoveAt (int index);

参数:

例外:如果索引小于0或等于或大于Count,则此方法将提供ArgumentOutOfRangeException

下面的程序说明了List .RemoveAt(Int32)方法的用法:

范例1:

// C# Program to remove the element at
// the specified index of the List
using System;
using System.Collections;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating an List of Integers
        List firstlist = new List();
  
        // Adding elements to List
        firstlist.Add(17);
        firstlist.Add(19);
        firstlist.Add(21);
        firstlist.Add(9);
        firstlist.Add(75);
        firstlist.Add(19);
        firstlist.Add(73);
  
        Console.WriteLine("Elements Present in List:\n");
  
        int p = 0;
  
        // Displaying the elements of List
        foreach(int k in firstlist)
        {
            Console.Write("At Position {0}: ", p);
            Console.WriteLine(k);
            p++;
        }
  
        Console.WriteLine(" ");
  
        // removing the element at index 3
        Console.WriteLine("Removing the element at index 3\n");
  
        // 9 will remove from the List
        // and 75 will come at index 3
        firstlist.RemoveAt(3);
  
        int p1 = 0;
  
        // Displaying the elements of List
        foreach(int n in firstlist)
        {
            Console.Write("At Position {0}: ", p1);
            Console.WriteLine(n);
            p1++;
        }
    }
}

输出:

Elements Present in List:

At Position 0: 17
At Position 1: 19
At Position 2: 21
At Position 3: 9
At Position 4: 75
At Position 5: 19
At Position 6: 73
 
Removing the element at index 3

At Position 0: 17
At Position 1: 19
At Position 2: 21
At Position 3: 75
At Position 4: 19
At Position 5: 73

范例2:

// C# Program to remove the element at
// the specified index of the List
using System;
using System.Collections;
using System.Collections.Generic;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // Creating an List of Integers
        List firstlist = new List();
  
        // Adding elements to List
        firstlist.Add(17);
        firstlist.Add(19);
        firstlist.Add(21);
        firstlist.Add(9);
        firstlist.Add(75);
        firstlist.Add(19);
        firstlist.Add(73);
  
        Console.WriteLine("Elements Present in List:\n");
  
        int p = 0;
  
        // Displaying the elements of List
        foreach(int k in firstlist)
        {
            Console.Write("At Position {0}: ", p);
            Console.WriteLine(k);
            p++;
        }
  
        Console.WriteLine(" ");
  
        // removing the element at index 3
        Console.WriteLine("Removing the element at index 3\n");
  
        // taking negative index
        // it will give error as index
        // cannot be less than 0
        firstlist.RemoveAt(-1);
  
        int p1 = 0;
  
        // Displaying the elements of List
        foreach(int n in firstlist)
        {
            Console.Write("At Position {0}: ", p1);
            Console.WriteLine(n);
            p1++;
        }
    }
}

运行时错误:

输出:

Elements Present in List:

At Position 0: 17
At Position 1: 19
At Position 2: 21
At Position 3: 9
At Position 4: 75
At Position 5: 19
At Position 6: 73
 
Removing the element at index 3

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.list-1.removeat?view=netframework-4.7.2