C#中的ArrayList

来源:互联网 发布:linux 改变所属组 编辑:程序博客网 时间:2024/06/14 03:52

原文地址:http://www.dingos.cn/index.php?topic=873.0

【ArrayList】
ArrayList非常类似于数字,也称为数组列表,ArrayList是可以非常直观地动态维护的。数组的容量是固定的,ArrayList的容量可以根据需要自动扩充,它的索引会根据扩展而重新进行分配和调整。ArrayList可以根据类提供的方法,进行访问、新增、删除元素的操作,实现对集合的动态访问。
使用大小可按需动态增加的数组实现 IList 接口。ArrayList类来自于System.Collections命名空间,在使用ArrayList之前一定要引入这个命名空间。注意ArrayList是动态可维护的,在第一时可以不指定容量,也可以指定容量。
      ArrayList myAL = new ArrayList();
      ArrayList myAL1 = new ArrayList(5);

注意:
不保证会对 ArrayList 排序。 在执行需要对 ArrayList 排序的操作之前,必须对 ArrayList 进行排序。
ArrayList 的容量是 ArrayList 可以保存的元素数。随着向 ArrayList 中添加元素,容量通过重新分配按需自动增加。可通过调用 TrimToSize 或通过显式设置 Capacity 属性减少容量。
使用整数索引可以访问此集合中的元素。 此集合中的索引从零开始。
ArrayList 接受 null引用作为有效值并且允许重复的元素。
ArrayList 只提供一维的形式。

【ArrayList常用属性 - Capacity、Count】
ArrayList.Capacity 属性:获取或设置 ArrayList 可包含的元素数。
ArrayList.Count 属性:获取 ArrayList 中实际包含的元素数。

Capacity 是 ArrayList 可以存储的元素数。Count 是 ArrayList 中实际存储的元素数。
Capacity 总是大于或等于 Count。如果在添加元素时 Count 超过 Capacity,则通过在复制旧元素和添加新元素之前重新分配内部数组来使容量自动增加。增长后的容量是原来的两倍。
可通过调用 TrimToSize 或通过显式设置 Capacity 属性减少容量。如果 Capacity 的值显式设置,则内部数组也需要重新分配以容纳指定的容量。
检索此属性值的运算复杂度为 O(1);设置此属性的运算复杂度为 O(n),其中,n 是新容量。

【ArrayList常用属性 - Item】
ArrayList.Item 属性:获取或设置指定索引处的元素。
参数
    index
    类型:System.Int32
    要获得或设置的元素从零开始的索引。
属性值
    类型:SystemObject
    指定索引处的元素。

注意:
ArrayList 接受 null 引用作为有效值并且允许重复的元素。
通过使用下面的语法,此属性提供访问集合中特定元素的能力:myCollection[index]。
C# 语言使用 this 关键字来定义索引器而不是通过实现 Item 属性。
检索此属性值的运算复杂度为 O(1);设置此属性的运算复杂度也是 O(1)。
        for (int i = 0; i < stringList.Count; i++) {
            Console.WriteLine("Element {0} is /"{1}/"", i, stringList[i]);
        }

【为ArrayList添加元素方法 - Add】
ArrayList.Add 方法:将对象添加到 ArrayList 的结尾处。
参数
    value
    类型:SystemObject
    要添加到 ArrayList 的末尾处的 Object。该值可以为 null 引用。
返回值
    类型:System.Int32
    ArrayList 索引,已在此处添加了 value。

      myAL.Add( "The" );
      myAL.Add( "quick" );
      myAL.Add( "brown" );
      myAL.Add( "fox" );

【为ArrayList添加元素方法 - AddRange】
ArrayList.AddRange 方法:将 ICollection 的元素添加到 ArrayList 的末尾。
参数
    c
    类型:System.Collections.ICollection
    ICollection,其元素应被添加到 ArrayList 的末尾。集合本身不能为 null引用,但它可以包含为 null 引用的元素。
      // Copies the Queue elements to the end of the ArrayList.
      myAL.AddRange( myQueue );

【为ArrayList添加元素方法 - Insert】
ArrayList.Insert 方法:将元素插入 ArrayList 的指定索引处。
参数
    index
    类型:System.Int32
    从零开始的索引,应在该位置插入 value。
    value
    类型:System.Object
    要插入的 Object。该值可以为 null 引用。
      myAL.Insert( 0, "The" );
      myAL.Insert( 1, "fox" );
      myAL.Insert( 2, "jumps" );
      myAL.Insert( 3, "over" );
      myAL.Insert( 4, "the" );
      myAL.Insert( 5, "dog" );

【为ArrayList添加元素方法 - InsertRange】
ArrayList.InsertRange 方法:将 ICollection 的元素添加到 ArrayList 的末尾。
参数
    index
    类型:System.Int32
    从零开始的索引,应在该位置插入 value。
    c
    类型:System.Collections.ICollection
    ICollection,应将其元素插入到 ArrayList 中。集合本身不能为 null 引用,但它可以包含为 null 引用的元素。
      // Creates and initializes a new Queue.
      Queue myQueue = new Queue();
      myQueue.Enqueue( "quick" );
      myQueue.Enqueue( "brown" );

      // Copies the Queue elements to the ArrayList at index 1.
      myAL.InsertRange( 1, myQueue );

【删除ArrayList中的元素 - Remove】
ArrayList.Remove 方法:从 ArrayList 中移除特定对象的第一个匹配项。
参数
    obj
    类型:System.Object
    要从 ArrayList 移除的 Object。该值可以为 null 引用。
      // Removes the element containing "lazy".
      myAL.Remove( "lazy" );

注意:
如果 ArrayList 不包含指定对象,则 ArrayList 保持不变。不引发异常。

【删除ArrayList中的元素 - RemoveRange】
ArrayList.RemoveRange 方法:从 ArrayList 中移除一定范围的元素。
参数
    index
    类型:System.Int32
    要移除的元素的从零开始的索引。
    count
    类型:System.Int32
    要移除的元素数
      myAL.RemoveRange( 4, 3 );

【删除ArrayList中的元素 - Clear】
ArrayList.Clear方法:从 ArrayList 中移除所有元素。
Count 被设置为零,并且对来自该集合元素的其他对象的引用也被释放。
Capacity 保持不变。
      myAL.Clear();

【ArrayList和数组】
  • ArrayList的特点:
    • Array 类的容量或元素数是固定的,而 ArrayList 类的容量可以根据需要动态扩展。通过设置ArrayList.Capacity 的值可以重新分配内存和复制元素
    • 使用 ArrayList 提供的方法可以同时添加、插入或移除一个范围内的元素
  • 数组灵活性:
    • 可以设置数组的下界
    • 数组可以有多个维
    • 许多需要使用数组的实例都可以使用ArrayList

原创粉丝点击