C# ArrayList类介绍和用法指南

2021年5月10日16:11:14 发表评论 828 次浏览

数组列表表示可以单独索引的对象的有序集合。它基本上是数组的替代方法。它还允许动态分配内存, 添加, 搜索和排序列表中的项目。

ArrayList类的属性:

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

构造函数

构造函数 描述
ArrayList() 初始化ArrayList类的新实例, 该实例为空并具有默认初始容量。
ArrayList(ICollection) 初始化ArrayList类的新实例, 该实例包含从指定集合复制的元素, 并且具有与复制的元素数量相同的初始容量。
ArrayList(Int32) 初始化ArrayList类的新实例, 该实例为空并具有指定的初始容量。

例子:

//C# code to create an 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();
  
         //Adding elements to ArrayList
         myList.Add( "Hello" );
         myList.Add( "World" );
  
         Console.WriteLine( "Count : " + myList.Count);
         Console.WriteLine( "Capacity : " + myList.Capacity);
     }
}

输出如下:

Count : 2
Capacity : 4

属性

属性 描述
Capacity 获取或设置ArrayList可以包含的元素数。
Count 获取实际包含在ArrayList中的元素数。
IsFixedSize 获取一个值, 该值指示ArrayList是否具有固定大小。
IsReadOnly 获取一个值, 该值指示ArrayList是否为只读。
IsSynchronized 获取一个值, 该值指示是否同步对ArrayList的访问(线程安全)。
Item[Int32] 获取或设置指定索引处的元素。
SyncRoot 获取一个对象, 该对象可用于同步对ArrayList的访问。

例子:

//C# program to illustrate the 
//ArrayList Class Properties
using System; 
using System.Collections; 
using System.Collections.Generic; 
    
class GFG { 
    
     //Driver code 
     public static void Main() 
     { 
    
         //Creating an ArrayList 
         ArrayList myList = new ArrayList(); 
    
         //Adding elements to ArrayList 
         myList.Add( "A" ); 
         myList.Add( "B" ); 
         myList.Add( "C" ); 
         myList.Add( "D" ); 
         myList.Add( "E" ); 
         myList.Add( "F" ); 
    
         //-------- IsFixedSize Property --------
          
         //To check if the ArrayList has fixed size or not 
         Console.WriteLine(myList.IsFixedSize); 
          
         //-------- IsReadOnly Property --------
          
         //To check if the ArrayList is read-only or not 
         Console.WriteLine(myList.IsReadOnly); 
     } 
}

输出如下:

False
False

方法

方法 描述
Adapter(IList) 为特定的IList创建一个ArrayList包装器。
Add(Object) 将一个对象添加到ArrayList的末尾。
AddRange(ICollection) 将ICollection的元素添加到ArrayList的末尾。
BinarySearch(Int32, Int32, Object, IComparer) 使用指定的比较器在排序的ArrayList中的元素范围内搜索元素, 并返回该元素的从零开始的索引。
BinarySearch(对象) 使用默认比较器在整个排序的ArrayList中搜索元素, 并返回该元素的从零开始的索引。
BinarySearch(Object, IComparer) 使用指定的比较器在整个排序的ArrayList中搜索元素, 然后返回该元素的从零开始的索引。
Clear() 从ArrayList中删除所有元素。
Clone() 创建ArrayList的浅表副本。
Contains(对象) 确定元素是否在ArrayList中。
CopyTo(Array) 从目标数组的开头开始, 将整个ArrayList复制到兼容的一维Array。
CopyTo(Array, Int32) 从目标数组的指定索引处开始, 将整个ArrayList复制到兼容的一维Array。
CopyTo(Int32, Array, Int32, Int32) 从目标数组的指定索引处开始, 将一系列元素从ArrayList复制到兼容的一维Array。
Equals(对象) 确定指定对象是否等于当前对象。
FixedSize(ArrayList) 返回具有固定大小的ArrayList包装器。
FixedSize(IList) 返回具有固定大小的IList包装器。
GetEnumerator() 返回整个ArrayList的枚举数。
GetEnumerator(Int32, Int32) 返回ArrayList中元素范围的枚举数。
GetHashCode() 用作默认哈希函数。
GetRange(Int32, Int32) 返回一个ArrayList, 它表示源ArrayList中元素的子集。
GetType() 获取当前实例的类型。
IndexOf(对象) 搜索指定的Object并返回整个ArrayList中第一次出现的从零开始的索引。
IndexOf(Object, Int32) 搜索指定的Object并返回从指定索引到最后一个元素的ArrayList元素范围内第一次出现的从零开始的索引。
IndexOf(Object, Int32, Int32) 搜索指定的Object, 并返回从ArrayList的元素范围开始的第一个从零开始的索引, 该元素范围从指定的索引开始, 并包含指定数量的元素。
Insert(Int32, 对象) 将元素插入到ArrayList中的指定索引处。
InsertRange(Int32, ICollection) 将集合的元素插入到指定索引处的ArrayList中。
LastIndexOf(Object) 搜索指定的Object并返回整个ArrayList中最后一次出现的从零开始的索引。
LastIndexOf(Object, Int32) 搜索指定的Object并返回从第一个元素到指定索引的ArrayList元素范围内最后一次出现的从零开始的索引。
LastIndexOf(Object, Int32, Int32) 搜索指定的Object并返回ArrayList中包含指定数量的元素并以指定索引结尾的元素范围内最后一次出现的从零开始的索引。
MemberwiseClone() 创建当前对象的浅表副本。
ReadOnly(ArrayList) 返回一个只读的ArrayList包装器。
只读(IList) 返回一个只读的IList包装器。
删除(对象) 从ArrayList中删除第一次出现的特定对象。
RemoveAt(Int32) 移除ArrayList指定索引处的元素。
RemoveRange(Int32, Int32) 从ArrayList中删除一系列元素。
Repeat(Object, Int32) 返回一个ArrayList, 其元素是指定值的副本。
Reverse() 颠倒整个ArrayList中元素的顺序。
Reverse(Int32, Int32) 反转指定范围内元素的顺序。
SetRange(Int32, ICollection) 在ArrayList中的一系列元素上复制集合的元素。
Sort() 对整个ArrayList中的元素进行排序。
Sort(IComparer) 使用指定的比较器对整个ArrayList中的元素进行排序。
Sort(Int32, Int32, IComparer) 使用指定的比较器对ArrayList中的一系列元素进行排序。
Synchronized(ArrayList) 返回一个已同步(线程安全)的ArrayList包装器。
Synchronized(IList) 返回一个已同步的IList包装器(线程安全)。
ToArray() 将ArrayList的元素复制到新的Object数组。
ToArray(类型) 将ArrayList的元素复制到指定元素类型的新数组。
ToString() 返回表示当前对象的字符串。
TrimToSize() 将容量设置为ArrayList中元素的实际数量。

示例1:

//C# code to check if an element is
//contained in ArrayList or not
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
     //Driver code
     public static void Main()
     {
  
         //Creating an ArrayList
         ArrayList myList = new ArrayList();
  
         //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" );
  
         //To check if the ArrayList Contains element "E"
         //If yes, then display it's index, else
         //display the message
         if (myList.Contains( "E" ))
             Console.WriteLine( "Yes, exists at index " + myList.IndexOf( "E" ));
         else
             Console.WriteLine( "No, doesn't exists" );
     }
}

输出如下:

Yes, exists at index 4

示例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: 
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?view=netframework-4.7.2

木子山

发表评论

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