C# |数组列表类
数组列表表示可以单独索引的对象的有序集合。它基本上是数组的替代。它还允许动态内存分配、添加、搜索和排序列表中的项目。
数组列表类的属性:
- 可以随时在数组列表集合中添加或删除元素。
- 数组列表不能保证排序。
- 数组列表的容量是数组列表可以容纳的元素数量。
- 可以使用整数索引访问此集合中的元素。此集合中的索引从零开始。
- 它还允许重复元素。
- 不支持在数组列表集合中使用多维数组作为元素。
构造器
| 构造器 | 描述 | | 数组列表()T3 | 初始化数组列表类的新实例,该实例为空并具有默认的初始容量。 | | 数组列表(ICollection) | 初始化数组列表类的新实例,该实例包含从指定集合复制的元素,并且具有与复制的元素数相同的初始容量。 | | 数组列表(Int32) | 初始化数组列表类的新实例,该实例为空并具有指定的初始容量。 |
示例:
// 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
性能
| 财产 | 描述 | | 运力 | 获取或设置数组列表可以包含的元素数量。 | | 计数 | 获取数组列表中实际包含的元素数量。 | | 【is fixed DSI】 | 获取一个值,该值指示数组列表是否具有固定大小。 | | 【isreadonly】 | 获取一个值,该值指示数组列表是否为只读。 | | 同步 | 获取一个值,该值指示对数组列表的访问是否同步(线程安全)。 | | 项【国际 32】 | 获取或设置指定索引处的元素。 | | 【sync root】 | 获取一个对象,该对象可用于同步对数组列表的访问。 |
示例:
// 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
方法
| 方法 | 描述 | | 适配器(IList) | 为特定 IList 创建数组列表包装。 | | 添加(对象) | 将对象添加到数组列表的末尾。 | | 【add range(icollion) | 将 ICollection 的元素添加到数组列表的末尾。 | | BinarySearch(Int32,Int32,Object,IComparer) | 使用指定的比较器在排序的数组列表中搜索某个元素的元素范围,并返回该元素的从零开始的索引。 | | 二元搜索(对象) | 使用默认比较器在整个排序数组列表中搜索元素,并返回元素的从零开始的索引。 | | BinarySearch(Object,IComparer) | 使用指定的比较器在整个排序数组列表中搜索元素,并返回该元素的从零开始的索引。 | | 晴() | 从数组列表中移除所有元素。 | | T1】克隆()T3】 | 创建数组列表的浅拷贝。 | | 包含(对象) | 确定元素是否在数组列表中。 | | 文案(阵) | 从目标数组的开头开始,将整个数组列表复制到兼容的一维数组中。 | | CopyTo(Array,Int32) | 从目标数组的指定索引开始,将整个数组列表复制到兼容的一维数组。 | | CopyTo(Int32,Array,Int32,Int32) | 从目标数组的指定索引开始,将数组列表中的一系列元素复制到兼容的一维数组中。 | | 等于(对象) | 确定指定的对象是否等于当前对象。 | | 固定大小(数组列表) | 返回一个固定大小的数组列表包装。 | | 固定大小(IList) | 返回一个固定大小的 IList 包装。 | | 【get 分子() | 返回整个数组列表的枚举数。 | | GetEnumerator(Int32,Int32) | 返回数组列表中某个元素范围的枚举数。 | | GetHashCode() | 用作默认哈希函数。 | | GetRange(Int32,Int32) | 返回一个数组列表,该列表代表源数组列表中元素的子集。 | | GetType() | 获取当前实例的类型。 | | 索引(对象) | 搜索指定的对象,并返回整个数组列表中第一个匹配项的从零开始的索引。 | | 索引(对象, Int32) | 搜索指定的对象,并返回数组列表中从指定索引延伸到最后一个元素的元素范围内第一个匹配项的从零开始的索引。 | | 索引 Of(对象,Int32,Int32) | 搜索指定的对象,并返回数组列表中元素范围内第一个匹配项的从零开始的索引,该索引从指定的索引开始并包含指定数量的元素。 | | 插入(Int32,Object) | 将元素插入数组列表中的指定索引处。 | | 插入范围(Int32,ICollection) | 将集合的元素插入到指定索引处的数组列表中。 | | 最后索引(对象) | 搜索指定的对象,并返回整个数组列表中最后一个匹配项的从零开始的索引。 | | 最后索引(对象, Int32) | 搜索指定的对象,并返回数组列表中从第一个元素延伸到指定索引的元素范围内最后一个匹配项的从零开始的索引。 | | 最后一个索引(对象,Int32,Int32) | 搜索指定的对象,并返回数组列表中元素范围内最后一个匹配项的从零开始的索引,该列表包含指定数量的元素并以指定的索引结束。 | | MemberWiseCrone() | 创建当前对象的浅拷贝。 | | 【只读(数组列表) | 返回只读数组列表包装。 | | 只读(IList) | 返回只读 IList 包装。 | | 移除(对象) | 从数组列表中删除特定对象的第一个匹配项。 | | 移除 At(Int32) | 移除数组列表中指定索引处的元素。 | | 移除范围(Int32,Int32) | 从数组列表中移除一系列元素。 | | 重复(对象,Int32) | 返回一个数组列表,其元素是指定值的副本。 | | 【反转() | 颠倒整个数组列表中元素的顺序。 | | 倒车(Int32,Int32) | 反转指定范围内元素的顺序。 | | 【setrange(int 32,icollion) | 将集合的元素复制到数组列表中的一系列元素上。 | | 【排序() | 对整个数组列表中的元素进行排序。 | | 【排序(IComparer) | 使用指定的比较器对整个数组列表中的元素进行排序。 | | 排序(Int32,Int32,IComparer) | 使用指定的比较器对数组列表中某个元素范围内的元素进行排序。 | | 【同步(数组列表) | 返回一个同步的数组列表包装(线程安全)。 | | 同步(IList) | 返回一个同步的 IList 包装(线程安全)。 | | 【toaarray() | 将数组列表的元素复制到新的对象数组中。 | | ToArray(类型) | 将数组列表的元素复制到指定元素类型的新数组中。 | | ToString() | 返回表示当前对象的字符串。 | | 【trimtosize() | 将容量设置为数组列表中元素的实际数量。 |
例 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
参考:
版权属于:月萌API www.moonapi.com,转载请注明出处