在C#代碼中使用一系列字符串(strings)並需要爲其創建一個列表時,List<string>泛型類是一個用於存儲一系列字 符串(strings)的極其優秀的解決辦法。下面一起有一些List<string>泛型類的示例,一起來看看吧。
List示例
下面是一個使用C#創建一個新的一系列字符串的列表的示例,利用foreach語句循環使用其每一個字符串。請注意在代碼片段的頂部添加所需的命名空間:“using System.Collections.Generic;”,List是該命名空間裏的一個泛型類型。
List<string>示例代碼:
1 using System;
2 using System.Collections.Generic;
3
4 class Program
5 {
6 static void Main()
7 {
8 List<string> cities = new List<string>(); // List of city names
9 cities.Add("San Diego"); // String element 1
10 cities.Add("Humboldt"); // 2
11 cities.Add("Los Angeles"); // 3
12 cities.Add("Auburn"); // 4
13
14 // Write each city string.
15 foreach (string city in cities)
16 {
17 Console.WriteLine(city);
18 }
19 Console.ReadKey();
20 }
21 }
輸出:
San Diego
Humboldt
Los Angeles
Auburn
注意代碼中的尖括號(angle brackets)。在聲明語句中尖括號<和>將string類型圍在中間,這意味着List僅能夠存儲String類型的元素。string類型可以是小寫字體的string,也可以使大寫字體的String。
使用Collection實現初始化示例
C#語法允許以一種更加清晰的辦法來實現List的初始化。使用collection進行初始化,必須使用大括號{}包圍作初始化用的值。下面示例中的註釋說明了在執行該程序時編譯器所使用的代碼。
List初始化示例代碼:
1 using System;
2 using System.Collections.Generic;
3
4 class Program
5 {
6 static void Main()
7 {
8 List<string> moths = new List<string>
9 {
10 "African armyworm",
11 "Mottled pug",
12 "Purple thug",
13 "Short-cloaked moth"
14 };
15 // The List moth contains four strings.
16 // IL:
17 //
18 // List<string> <>g__initLocal0 = new List<string>();
19 // <>g__initLocal0.Add("African armyworm");
20 // // ... four more Add calls
21 // List<string> moths = <>g__initLocal0;
22 }
23 }
一、List<T>描述
1)、表示可通過索引訪問的對象的強類型列表;提供用於對列表進行搜索、排序和操作的方法。2)、是ArrayList類的泛型等效類。3)、可以使用一個整數索引訪問此集合中的元素;索引從
零 開始。4)、可以接收null空引用(VB中的Nothing)。5)、允許重複元素
二、List<T>構造函數
List<T>()
初始化 List<T> 類的新實例,該實例爲空並且具有默認初始容量(0)。
List<T>(IEnumerable<T>)
初始化 List<T> 類的新實例,該實例包含從指定集合複製的元素並且具有足夠的容量來容納所複製的元素。
List<T>(Int32)
始化 List<T> 類的新實例,該實例爲空並且具有指定的初始容量。
三、List<T>屬性
Capacity
獲取或設置該內部數據結構在不調整大小的情況下能夠容納的元素總數。
Count
獲取 List<T> 中實際包含的元素數。
四、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> 複製到兼容的一維數組中,從目標數組的開頭開始放置。
Exists
確定 List<T> 是否包含與指定謂詞所定義的條件相匹配的元素。
Find
搜索與指定謂詞所定義的條件相匹配的元素,並返回整個 List<T> 中的第一個匹配元素。
FindIndex(Predicate<T>)
搜索與指定謂詞所定義的條件相匹配的元素,並返回整個 List<T> 中第一個匹配元素的從零開始的索引。
ForEach
對 List<T> 的每個元素執行指定操作。
GetEnumerator
返回循環訪問 List<T> 的枚舉器。
IndexOf(T)
搜索指定的對象,並返回整個 List<T> 中第一個匹配項的從零開始的索引。
Insert
將元素插入 List<T> 的指定索引處。
InsertRange
將集合中的某個元素插入 List<T> 的指定索引處。
LastIndexOf(T)
搜索指定的對象,並返回整個 List<T> 中最後一個匹配項的從零開始的索引。
Remove
從 List<T> 中移除特定對象的第一個匹配項。
Reverse()
將整個 List<T> 中元素的順序反轉。
Sort()
使用默認比較器對整個 List<T> 中的元素進行排序。
五、常用方法實例
(1)創建及初始化:
List<string> myList = new
List<string>();//初始Capacity爲 零List<string> myList2 = new
List<string>(30); //初始Capacity爲 30List<string> myList3 = new
List<string>(new string[] { "1", "a", "2", "b" });//初始Capacity 爲
4,並且元素已被複制
(2)添加一個元素 List.Add(T item)
mlist.Add("d");
(3)添加集合元素
string[] Arr2
={"f","g"."h"};mlist.AddRange(Arr2);
(4)在index位置添加一個元素 Insert(int index,T item)
mlist.Insert(1,"p");
(5)遍歷List中元素
foreach(T element in mlist)
T的類型與mlist聲明時一樣{ Console.WriteLine(element);}
(6)刪除元素
List.Remove(T item)
刪除一個值mlist.Remove("a");
List.RemoveAt(int
index);刪除下標爲index的元素mlist.RemoveAt(0);List.RemoveRange(int index,int
count); 下標index開始,刪除count個元素mlist.RemoveRange(3,2);