ArrayList 類別

定義

使用大小會視需要動態增加的陣列,實作 IList 介面。

public ref class ArrayList : System::Collections::IList
public ref class ArrayList : ICloneable, System::Collections::IList
public class ArrayList : System.Collections.IList
public class ArrayList : ICloneable, System.Collections.IList
[System.Serializable]
public class ArrayList : ICloneable, System.Collections.IList
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class ArrayList : ICloneable, System.Collections.IList
type ArrayList = class
    interface IEnumerable
    interface IList
    interface ICollection
type ArrayList = class
    interface ICollection
    interface IEnumerable
    interface IList
    interface ICloneable
type ArrayList = class
    interface IEnumerable
    interface IList
    interface ICollection
    interface ICloneable
[<System.Serializable>]
type ArrayList = class
    interface IList
    interface ICollection
    interface IEnumerable
    interface ICloneable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ArrayList = class
    interface IList
    interface ICollection
    interface IEnumerable
    interface ICloneable
Public Class ArrayList
Implements IList
Public Class ArrayList
Implements ICloneable, IList
繼承
ArrayList
衍生
屬性
實作

範例

下列範例示範如何建立和初始化 , ArrayList 以及如何顯示其值。

using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myList );
int main()
{
   
   // Creates and initializes a new ArrayList.
   ArrayList^ myAL = gcnew ArrayList;
   myAL->Add( "Hello" );
   myAL->Add( "World" );
   myAL->Add( "!" );
   
   // Displays the properties and values of the ArrayList.
   Console::WriteLine( "myAL" );
   Console::WriteLine( "    Count:    {0}", myAL->Count );
   Console::WriteLine( "    Capacity: {0}", myAL->Capacity );
   Console::Write( "    Values:" );
   PrintValues( myAL );
}

void PrintValues( IEnumerable^ myList )
{
   IEnumerator^ myEnum = myList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::Write( "   {0}", obj );
   }

   Console::WriteLine();
}

/* 
This code produces output similar to the following:

myAL
    Count:    3
    Capacity: 4
    Values:   Hello   World   !

*/
using System;
using System.Collections;
public class SamplesArrayList  {

   public static void Main()  {

      // Creates and initializes a new ArrayList.
      ArrayList myAL = new ArrayList();
      myAL.Add("Hello");
      myAL.Add("World");
      myAL.Add("!");

      // Displays the properties and values of the ArrayList.
      Console.WriteLine( "myAL" );
      Console.WriteLine( "    Count:    {0}", myAL.Count );
      Console.WriteLine( "    Capacity: {0}", myAL.Capacity );
      Console.Write( "    Values:" );
      PrintValues( myAL );
   }

   public static void PrintValues( IEnumerable myList )  {
      foreach ( Object obj in myList )
         Console.Write( "   {0}", obj );
      Console.WriteLine();
   }
}


/*
This code produces output similar to the following:

myAL
    Count:    3
    Capacity: 4
    Values:   Hello   World   !

*/
Imports System.Collections

Public Class SamplesArrayList    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new ArrayList.
        Dim myAL As New ArrayList()
        myAL.Add("Hello")
        myAL.Add("World")
        myAL.Add("!")
        
        ' Displays the properties and values of the ArrayList.
        Console.WriteLine("myAL")
        Console.WriteLine("    Count:    {0}", myAL.Count)
        Console.WriteLine("    Capacity: {0}", myAL.Capacity)
        Console.Write("    Values:")
        PrintValues(myAL)
    End Sub

    Public Shared Sub PrintValues(myList As IEnumerable)
        Dim obj As [Object]
        For Each obj In  myList
            Console.Write("   {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub

End Class


' This code produces output similar to the following:
' 
' myAL
'     Count:    3
'     Capacity: 4
'     Values:   Hello   World   !

備註

重要

不建議您將 類別用於 ArrayList 新的開發。 相反地,建議您使用泛型 List<T> 類別。 類別 ArrayList 的設計目的是要保存物件的異質集合。 不過,它不一定會提供最佳效能。 相反地,我們建議使用下列專案:

  • 如果是對象的異質集合,請使用 List<Object> C#) 中的 (,或在 List(Of Object) Visual Basic) 類型中使用 (。
  • 針對物件的同質集合,請使用 類別 List<T> 。 如需這些類別相對效能的討論,請參閱參考主題中的List<T>效能考慮。 如需 使用泛型 而非非泛型集合類型的一般資訊,請參閱不應在 GitHub 上使用非泛型集合。

ArrayList不保證會排序 。 ArrayList您必須先呼叫 其 Sort 方法,才能執行 (,例如BinarySearch需要ArrayList排序 的) 。 若要維護自動排序為加入新元素的集合,您可以使用 SortedSet<T> 類別。

ArrayList 容量是 可以保留的項目 ArrayList 數目。 隨著元素新增至 ArrayList,容量會自動隨著重新配置而增加。 您可以藉由呼叫 TrimToSize 或明確設定 Capacity 屬性來減少容量。

僅限 .NET Framework:對於非常大ArrayList的物件,您可以在運行時間環境中將組態專案的 屬性<gcAllowVeryLargeObjects>設定enabledtrue ,將 64 位系統上的最大容量增加到 20 億個專案。

此集合中的專案可以使用整數索引來存取。 此集合中的索引是以零起始。

集合 ArrayList 接受 null 做為有效值。 它也允許重複的專案。

不支援使用多維度陣列作為集合中的 ArrayList 專案。

建構函式

ArrayList()

初始化 ArrayList 類別的新執行個體,這個執行個體為空白且具有預設的初始容量。

ArrayList(ICollection)

初始化 ArrayList 類別的新執行個體,其含有從指定的集合複製過來的項目且具有與複製項目數一樣的初始容量。

ArrayList(Int32)

為具有指定初始容量且為空的 ArrayList 類別,初始化新的執行個體。

屬性

Capacity

取得或設定 ArrayList 可包含的項目數目。

Count

取得 ArrayList 中實際包含的項目數目。

IsFixedSize

取得值,指出 ArrayList 是否有固定的大小。

IsReadOnly

取得值,指出 ArrayList 是否唯讀。

IsSynchronized

取得值,這個值表示對 ArrayList 的存取是否同步 (安全執行緒)。

Item[Int32]

在指定的索引位置上取得或設定項目。

SyncRoot

取得可用以同步存取 ArrayList 的物件。

方法

Adapter(IList)

建立特定 IListArrayList 包裝函式。

Add(Object)

將物件加入至 ArrayList 的末端。

AddRange(ICollection)

ICollection 的項目加入 ArrayList 的結尾。

BinarySearch(Int32, Int32, Object, IComparer)

使用指定的比較子在已經過排序之 ArrayList 內,搜尋某範圍的項目,並傳回該項目以零為起始的索引。

BinarySearch(Object)

使用預設的比較子並傳回項目以零為起始的索引,來搜尋項目之整個排序的 ArrayList

BinarySearch(Object, IComparer)

使用指定的比較子並傳回項目以零為起始的索引,來搜尋項目之整個排序的 ArrayList

Clear()

移除 ArrayList 中的所有項目。

Clone()

建立 ArrayList 的淺層複本。

Contains(Object)

判斷某項目是否在 ArrayList 中。

CopyTo(Array)

從目標陣列的開頭開始,將整個 ArrayList 複製至相容的一維 Array

CopyTo(Array, Int32)

從目標陣列的指定索引開始,將整個 ArrayList 複製到相容的一維 Array

CopyTo(Int32, Array, Int32, Int32)

從目標陣列的指定索引開始,將項目範圍從 ArrayList 複製至相容的一維 Array

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
FixedSize(ArrayList)

傳回具有固定大小的 ArrayList 包裝函式。

FixedSize(IList)

傳回具有固定大小的 IList 包裝函式。

GetEnumerator()

傳回整個 ArrayList 的列舉值。

GetEnumerator(Int32, Int32)

傳回適用於 ArrayList 中某段範圍項目的列舉程式。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetRange(Int32, Int32)

傳回 ArrayList,代表來源 ArrayList 中項目的子集。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IndexOf(Object)

搜尋指定的 Object,並傳回在整個 ArrayList 中第一個符合項目之以零為起始的索引。

IndexOf(Object, Int32)

ArrayList 中從指定的索引開始到最後一個項目這段範圍內,搜尋指定的 Object 第一次出現的位置,並傳回其索引值 (索引以零起始)。

IndexOf(Object, Int32, Int32)

ArrayList 中從指定索引開始且包含指定項目個數的範圍內,搜尋指定的 Object 並傳回第一次出現的以零為起始的索引。

Insert(Int32, Object)

將項目插入至 ArrayList 中指定的索引位置。

InsertRange(Int32, ICollection)

將集合的項目插入位於指定索引的 ArrayList 中。

LastIndexOf(Object)

搜尋指定的 Object,並傳回在整個 ArrayList 中最後一個符合項目之以零為起始的索引。

LastIndexOf(Object, Int32)

ArrayList 中從第一個項目開始到指定的索引這個範圍內,搜尋指定的 Object,並傳回最後一次出現的索引值 (以零為起始)。

LastIndexOf(Object, Int32, Int32)

搜尋指定的 Object,並傳回 ArrayList 中包含指定之項目數且結束於指定之索引的項目範圍內,最後一個相符項目之以零為起始的索引。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ReadOnly(ArrayList)

傳回唯讀的 ArrayList 包裝函式。

ReadOnly(IList)

傳回唯讀的 IList 包裝函式。

Remove(Object)

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(Type)

ArrayList 的項目複製到指定項目類型的新陣列。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
TrimToSize()

將容量設為 ArrayList 中的實際項目數目。

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

執行緒安全性

Visual Basic 中的公用靜態 (Shared) 此類型的成員是安全線程。 並非所有的執行個體成員都是安全執行緒。

ArrayList可以同時支援多個讀取器,只要集合未修改即可。 若要保證的 ArrayList線程安全性,所有作業都必須透過 方法傳 Synchronized(IList) 回的包裝函式來完成。

透過集合進行列舉在本質上並非安全執行緒程序。 即使集合經過同步化,其他的執行緒仍可修改該集合,使列舉值擲回例外狀況。 若要保證列舉過程的執行緒安全,您可以在整個列舉過程中鎖定集合,或攔截由其他執行緒的變更所造成的例外狀況。

另請參閱