📜  C#| ArrayList类

📅  最后修改于: 2021-05-29 19:20:14             🧑  作者: Mango

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

ArrayList类的属性:

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

建设者

Constructor Description
ArrayList() Initializes a new instance of the ArrayList class that is empty and has the default initial capacity.
ArrayList(ICollection) Initializes a new instance of the ArrayList class that contains elements copied from the specified collection and that has the same initial capacity as the number of elements copied.
ArrayList(Int32) Initializes a new instance of the ArrayList class that is empty and has the specified initial capacity.

例子:

// 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

特性

Property Description
Capacity Gets or sets the number of elements that the ArrayList can contain.
Count Gets the number of elements actually contained in the ArrayList.
IsFixedSize Gets a value indicating whether the ArrayList has a fixed size.
IsReadOnly Gets a value indicating whether the ArrayList is read-only.
IsSynchronized Gets a value indicating whether access to the ArrayList is synchronized (thread safe).
Item[Int32] Gets or sets the element at the specified index.
SyncRoot Gets an object that can be used to synchronize access to the 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

方法

Method Description
Adapter(IList) Creates an ArrayList wrapper for a specific IList.
Add(Object) Adds an object to the end of the ArrayList.
AddRange(ICollection) Adds the elements of an ICollection to the end of the ArrayList.
BinarySearch(Int32, Int32, Object, IComparer) Searches a range of elements in the sorted ArrayList for an element using the specified comparer and returns the zero-based index of the element.
BinarySearch(Object) Searches the entire sorted ArrayList for an element using the default comparer and returns the zero-based index of the element.
BinarySearch(Object, IComparer) Searches the entire sorted ArrayList for an element using the specified comparer and returns the zero-based index of the element.
Clear() Removes all elements from the ArrayList.
Clone() Creates a shallow copy of the ArrayList.
Contains(Object) Determines whether an element is in the ArrayList.
CopyTo(Array) Copies the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array.
CopyTo(Array, Int32) Copies the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.
CopyTo(Int32, Array, Int32, Int32) Copies a range of elements from the ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.
Equals(Object) Determines whether the specified object is equal to the current object.
FixedSize(ArrayList) Returns an ArrayList wrapper with a fixed size.
FixedSize(IList) Returns an IList wrapper with a fixed size.
GetEnumerator() Returns an enumerator for the entire ArrayList.
GetEnumerator(Int32, Int32) Returns an enumerator for a range of elements in the ArrayList.
GetHashCode() Serves as the default hash function.
GetRange(Int32, Int32) Returns an ArrayList which represents a subset of the elements in the source ArrayList.
GetType() Gets the Type of the current instance.
IndexOf(Object) Searches for the specified Object and returns the zero-based index of the first occurrence within the entire ArrayList.
IndexOf(Object, Int32) Searches for the specified Object and returns the zero-based index of the first occurrence within the range of elements in the ArrayList that extends from the specified index to the last element.
IndexOf(Object, Int32, Int32) Searches for the specified Object and returns the zero-based index of the first occurrence within the range of elements in the ArrayList that starts at the specified index and contains the specified number of elements.
Insert(Int32, Object) Inserts an element into the ArrayList at the specified index.
InsertRange(Int32, ICollection) Inserts the elements of a collection into the ArrayList at the specified index.
LastIndexOf(Object) Searches for the specified Object and returns the zero-based index of the last occurrence within the entire ArrayList.
LastIndexOf(Object, Int32) Searches for the specified Object and returns the zero-based index of the last occurrence within the range of elements in the ArrayList that extends from the first element to the specified index.
LastIndexOf(Object, Int32, Int32) Searches for the specified Object and returns the zero-based index of the last occurrence within the range of elements in the ArrayList that contains the specified number of elements and ends at the specified index.
MemberwiseClone() Creates a shallow copy of the current Object.
ReadOnly(ArrayList) Returns a read-only ArrayList wrapper.
ReadOnly(IList) Returns a read-only IList wrapper.
Remove(Object) Removes the first occurrence of a specific object from the ArrayList.
RemoveAt(Int32) Removes the element at the specified index of the ArrayList.
RemoveRange(Int32, Int32) Removes a range of elements from the ArrayList.
Repeat(Object, Int32) Returns an ArrayList whose elements are copies of the specified value.
Reverse() Reverses the order of the elements in the entire ArrayList.
Reverse(Int32, Int32) Reverses the order of the elements in the specified range.
SetRange(Int32, ICollection) Copies the elements of a collection over a range of elements in the ArrayList.
Sort() Sorts the elements in the entire ArrayList.
Sort(IComparer) Sorts the elements in the entire ArrayList using the specified comparer.
Sort(Int32, Int32, IComparer) Sorts the elements in a range of elements in ArrayList using the specified comparer.
Synchronized(ArrayList) Returns an ArrayList wrapper that is synchronized (thread safe).
Synchronized(IList) Returns an IList wrapper that is synchronized (thread safe).
ToArray() Copies the elements of the ArrayList to a new Object array.
ToArray(Type) Copies the elements of the ArrayList to a new array of the specified element type.
ToString() Returns a string that represents the current object.
TrimToSize() Sets the capacity to the actual number of elements in the 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/zh-cn/dotnet/api/system.collections.arraylist?view=netframework-4.7.2