📌  相关文章
📜  C#|从ArrayList中删除一系列元素

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

ArrayList表示可以单独索引的对象的有序集合。基本上,它是数组的替代方法。它还允许动态分配内存,添加,搜索和排序列表中的项目。 ArrayList.RemoveRange(Int32,Int32)方法用于从ArrayList中删除一系列元素。

ArrayList类的属性:

  • 可以随时在数组列表集合中添加或删除元素。
  • 不能保证对ArrayList进行排序。
  • ArrayList的容量是ArrayList可以容纳的元素数。
  • 可以使用整数索引访问此集合中的元素。此集合中的索引从零开始。
  • 它还允许重复的元素。
  • 不支持将多维数组用作ArrayList集合中的元素。

句法:

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

参数:

例外情况:

  • ArgumentOutOfRangeException:如果index小于零或count小于零。
  • ArgumentException:如果index和count不表示ArrayList中元素的有效范围。
  • NotSupportedException:如果ArrayList为只读或ArrayList具有固定大小。

注意:此方法是O(n)运算,其中n是Count。

下面的程序说明了ArrayList.RemoveRange(Int32,Int32)方法的用法:

范例1:

// C# code to remove a range of
// elements from the ArrayList
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating an ArrayList
        ArrayList myList = new ArrayList(10);
  
        // Adding elements to ArrayList
        myList.Add("A");
        myList.Add("B");
        myList.Add("C");
        myList.Add("D");
        myList.Add("E");
        myList.Add("F");
        myList.Add("G");
        myList.Add("H");
        myList.Add("I");
        myList.Add("J");
  
        // Displaying the elements in ArrayList
        Console.WriteLine("The initial ArrayList is: ");
  
        foreach(string str in myList)
        {
            Console.WriteLine(str);
        }
  
        // removing 2 elements starting from index 2
        myList.RemoveRange(2, 2);
  
        // Displaying the modified ArrayList
        Console.WriteLine("The ArrayList after Removing elements: ");
  
        // Displaying the elements in ArrayList
        foreach(string str in myList)
        {
            Console.WriteLine(str);
        }
    }
}
输出:
The initial ArrayList is: 
A
B
C
D
E
F
G
H
I
J
The ArrayList after Removing elements: 
A
B
E
F
G
H
I
J

范例2:

// C# code to remove a range of
// elements from the ArrayList
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating an ArrayList
        ArrayList myList = new ArrayList(10);
  
        // Adding elements to ArrayList
        myList.Add(2);
        myList.Add(4);
        myList.Add(6);
        myList.Add(8);
        myList.Add(10);
        myList.Add(12);
        myList.Add(14);
        myList.Add(16);
        myList.Add(18);
        myList.Add(20);
  
        // Displaying the elements in ArrayList
        Console.WriteLine("The initial ArrayList: ");
  
        foreach(int i in myList)
        {
            Console.WriteLine(i);
        }
  
        // removing 4 elements starting from index 0
        myList.RemoveRange(0, 4);
  
        // Displaying the modified ArrayList
        Console.WriteLine("The ArrayList after Removing elements: ");
  
        // Displaying the elements in ArrayList
        foreach(int i in myList)
        {
            Console.WriteLine(i);
        }
    }
}
输出:
The initial ArrayList is: 
2
4
6
8
10
12
14
16
18
20
The ArrayList after Removing elements: 
10
12
14
16
18
20

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.arraylist.removerange?view=netframework-4.7.2