C#从ArrayList中删除一系列元素

2021年5月8日16:21:56 发表评论 760 次浏览

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

ArrayList类的属性:

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

语法如下:

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

参数:

index:它是要删除的元素范围的从零开始的索引。 count:这是要删除的元素数。

例外情况:

  • ArgumentOutOfRangeException:如果索引小于零或计数小于零。
  • 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/en-us/dotnet/api/system.collections.arraylist.removerange?view=netframework-4.7.2

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: