- F# 基礎教程
- F# - 首頁
- F# - 概述
- F# - 環境設定
- F# - 程式結構
- F# - 基本語法
- F# - 資料型別
- F# - 變數
- F# - 運算子
- F# - 決策
- F# - 迴圈
- F# - 函式
- F# - 字串
- F# - 可選項
- F# - 元組
- F# - 記錄
- F# - 列表
- F# - 序列
- F# - 集合
- F# - 對映
- F# - 判別聯合
- F# - 可變資料
- F# - 陣列
- F# - 可變列表
- F# - 可變字典
- F# - 基本輸入/輸出
- F# - 泛型
- F# - 委託
- F# - 列舉
- F# - 模式匹配
- F# - 異常處理
- F# - 類
- F# - 結構體
- F# - 運算子過載
- F# - 繼承
- F# - 介面
- F# - 事件
- F# - 模組
- F# - 名稱空間
F# - 可變列表
List<'T> 類表示一個強型別物件列表,可以透過索引訪問。
它是 List 類的可變對應物。它類似於陣列,因為它可以透過索引訪問,但是,與陣列不同的是,列表可以調整大小。因此,您無需在宣告期間指定大小。
建立可變列表
使用new關鍵字並呼叫列表的建構函式來建立列表。以下示例演示了這一點:
(* Creating a List *)
open System.Collections.Generic
let booksList = new List<string>()
booksList.Add("Gone with the Wind")
booksList.Add("Atlas Shrugged")
booksList.Add("Fountainhead")
booksList.Add("Thornbirds")
booksList.Add("Rebecca")
booksList.Add("Narnia")
booksList |> Seq.iteri (fun index item -> printfn "%i: %s" index booksList.[index])
編譯並執行程式時,將產生以下輸出:
0: Gone with the Wind 1: Atlas Shrugged 2: Fountainhead 3: Thornbirds 4: Rebecca 5: Narnia
List(T) 類
List(T) 類表示一個強型別物件列表,可以透過索引訪問。它提供用於搜尋、排序和操作列表的方法。
下表提供了 List(T) 類的屬性、建構函式和方法:
屬性
| 屬性 | 描述 |
|---|---|
| Capacity | 獲取或設定內部資料結構在不調整大小的情況下可以容納的元素總數。 |
| Count | 獲取 List(T) 中包含的元素數。 |
| Item | 獲取或設定指定索引處的元素。 |
建構函式
| 建構函式 | 描述 |
|---|---|
| List(T)() | 初始化一個新的空 List(T) 類例項,並具有預設初始容量。 |
| List(T)(IEnumerable(T)) | 初始化一個新的 List(T) 類例項,其中包含從指定集合複製的元素,並且具有足夠的容量來容納複製的元素數。 |
| List(T)(Int32) | 初始化一個新的空 List(T) 類例項,並具有指定的初始容量。 |
方法
| 方法 | 描述 |
|---|---|
| Add | 將物件新增到 List(T) 的末尾。 |
| AddRange | 將指定集合的元素新增到 List(T) 的末尾。 |
| AsReadOnly | 返回當前集合的只讀 IList(T) 包裝器。 |
| BinarySearch(T) | 使用預設比較器搜尋整個已排序的 List(T) 中的元素,並返回元素的基於零的索引。 |
| BinarySearch(T, IComparer(T)) | 使用指定的比較器搜尋整個已排序的 List(T) 中的元素,並返回元素的基於零的索引。 |
| BinarySearch(Int32, Int32, T, IComparer(T)) | 使用指定的比較器搜尋已排序 List(T) 中的元素範圍,並返回元素的基於零的索引。 |
| Clear | 從 List(T) 中移除所有元素。 |
| Contains | 確定 List(T) 中是否包含元素。 |
| ConvertAll(TOutput) | 將當前 List(T) 中的元素轉換為另一種型別,並返回包含已轉換元素的列表。 |
| CopyTo(T[]) | 將整個 List(T) 複製到相容的一維陣列中,從目標陣列的開頭開始。 |
| CopyTo(T[], Int32) | 將整個 List(T) 複製到相容的一維陣列中,從目標陣列的指定索引開始。 |
| CopyTo(Int32, T[], Int32, Int32) | 將 List(T) 中的元素範圍複製到相容的一維陣列中,從目標陣列的指定索引開始。 |
| Equals(Object) | 確定指定的物件是否等於當前物件。(繼承自 Object。) |
| Exists | 確定 List(T) 是否包含與指定謂詞定義的條件匹配的元素。 |
| Finalize | 允許物件嘗試釋放資源並在垃圾回收器回收它之前執行其他清理操作(繼承自 Object)。 |
| Find | 搜尋與指定謂詞定義的條件匹配的元素,並返回整個 List(T) 中的第一次出現。 |
| FindAll | 檢索與指定謂詞定義的條件匹配的所有元素。 |
| FindIndex(Predicate(T)) | 搜尋與指定謂詞定義的條件匹配的元素,並返回整個 List(T) 中第一次出現的基於零的索引。 |
| FindIndex(Int32, Predicate(T)) | 搜尋與指定謂詞定義的條件匹配的元素,並返回 List(T) 中從指定索引到最後一個元素的元素範圍內第一次出現的基於零的索引。 |
| FindIndex(Int32, Int32, Predicate(T)) | 搜尋與指定謂詞定義的條件匹配的元素,並返回 List(T) 中從指定索引開始幷包含指定數量元素的元素範圍內第一次出現的基於零的索引。 |
| FindLast | 搜尋與指定謂詞定義的條件匹配的元素,並返回整個 List(T) 中的最後一次出現。 |
| FindLastIndex(Predicate(T)) | 搜尋與指定謂詞定義的條件匹配的元素,並返回整個 List(T) 中最後一次出現的基於零的索引。 |
| FindLastIndex(Int32, Predicate(T)) | 搜尋與指定謂詞定義的條件匹配的元素,並返回 List(T) 中從第一個元素到指定索引的元素範圍內最後一次出現的基於零的索引。 |
| FindLastIndex(Int32, Int32, Predicate(T)) | 搜尋與指定謂詞定義的條件匹配的元素,並返回 List(T) 中包含指定數量元素並以指定索引結束的元素範圍內最後一次出現的基於零的索引。 |
| ForEach | 對 List(T) 的每個元素執行指定的動作。 |
| GetEnumerator | 返回一個迭代 List(T) 的列舉器。 |
| GetHashCode | 用作預設雜湊函式。(繼承自 Object。) |
| GetRange | 建立源 List(T) 中元素範圍的淺表副本。 |
| GetType | 獲取當前例項的 Type。(繼承自 Object。) |
| IndexOf(T) | 搜尋指定的物件並返回整個 List(T) 中第一次出現的基於零的索引。 |
| IndexOf(T, Int32) | 搜尋指定的物件並返回 List(T) 中從指定索引到最後一個元素的元素範圍內第一次出現的基於零的索引。 |
| IndexOf(T, Int32, Int32) | 搜尋指定的物件並返回 List(T) 中從指定索引開始幷包含指定數量元素的元素範圍內第一次出現的基於零的索引。 |
| Insert | 在 List(T) 的指定索引處插入元素。 |
| InsertRange | 在 List(T) 的指定索引處插入集合的元素。 |
| LastIndexOf(T) | 搜尋指定的物件並返回整個 List(T) 中最後一次出現的基於零的索引。 |
| LastIndexOf(T, Int32) | 搜尋指定的物件並返回 List(T) 中從第一個元素到指定索引的元素範圍內最後一次出現的基於零的索引。 |
| LastIndexOf(T, Int32, Int32) | 搜尋指定的物件並返回 List(T) 中包含指定數量元素並以指定索引結束的元素範圍內最後一次出現的基於零的索引。 |
| MemberwiseClone | 建立當前 Object 的淺表副本。(繼承自 Object。) |
| Remove | 從 List(T) 中移除特定物件的第一次出現。 |
| RemoveAll | 移除與指定謂詞定義的條件匹配的所有元素。 |
| RemoveAt | 移除 List(T) 指定索引處的元素。 |
| RemoveRange | 從 List(T) 中移除一系列元素。 |
| Reverse() | 反轉整個 List(T) 中元素的順序。 |
| Reverse(Int32, Int32) | 反轉指定範圍內的元素順序。 |
| Sort() | 使用預設比較器對整個 List(T) 中的元素進行排序。 |
| Sort(Comparison(T)) | 使用指定的 System.Comparison(T) 對整個 List(T) 中的元素進行排序。 |
| Sort(IComparer(T)) | 使用指定的比較器對整個 List(T) 中的元素進行排序。 |
| Sort(Int32, Int32, IComparer(T)) | 使用指定的比較器對 List(T) 中一系列元素進行排序。 |
| ToArray | 將 List(T) 的元素複製到一個新陣列中。 |
| ToString | 返回表示當前物件的字串。(繼承自 Object。) |
| TrimExcess | 如果該數字小於閾值,則將容量設定為 List(T) 中元素的實際數量。 |
| TrueForAll | 確定 List(T) 中的每個元素是否都與指定謂詞定義的條件匹配。 |
示例
(* Creating a List *)
open System.Collections.Generic
let booksList = new List<string>()
booksList.Add("Gone with the Wind")
booksList.Add("Atlas Shrugged")
booksList.Add("Fountainhead")
booksList.Add("Thornbirds")
booksList.Add("Rebecca")
booksList.Add("Narnia")
printfn"Total %d books" booksList.Count
booksList |> Seq.iteri (fun index item -> printfn "%i: %s" index booksList.[index])
booksList.Insert(2, "Roots")
printfn("after inserting at index 2")
printfn"Total %d books" booksList.Count
booksList |> Seq.iteri (fun index item -> printfn "%i: %s" index booksList.[index])
booksList.RemoveAt(3)
printfn("after removing from index 3")
printfn"Total %d books" booksList.Count
booksList |> Seq.iteri (fun index item -> printfn "%i: %s" index booksList.[index])
編譯並執行程式時,將產生以下輸出:
Total 6 books 0: Gone with the Wind 1: Atlas Shrugged 2: Fountainhead 3: Thornbirds 4: Rebecca 5: Narnia after inserting at index 2 Total 7 books 0: Gone with the Wind 1: Atlas Shrugged 2: Roots 3: Fountainhead 4: Thornbirds 5: Rebecca 6: Narnia after removing from index 3 Total 6 books 0: Gone with the Wind 1: Atlas Shrugged 2: Roots 3: Thornbirds 4: Rebecca 5: Narnia
廣告