<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # C# 集合 > 原文: [https://zetcode.com/lang/csharp/collections/](https://zetcode.com/lang/csharp/collections/) 在本章中,我們將介紹 C# 集合。 .NET 框架為數據存儲和檢索提供了專門的類。 在前面的章節中,我們描述了數組。 集合是對數組的增強。 C# 中有三種不同的集合類型: * 標準 * 泛型 * 并發 標準集合可在`System.Collections`下找到。 它們不將元素存儲為特定類型的對象,而是存儲為`Object`類型的對象。 標準集合包括`ArrayList`,`Hashtable`,`Queue`和`Stack`。 The generic collections are found under `System.Collections.Generic`. Generic collections are more flexible and are the preferred way to work with data. Generics enhance code reuse, type safety, and performance. The generic collections include `Dictionary<T, T>`, `List<T>`, `Queue<T>`, `SortedList<T>`, and `Stack<T>`. 并發集合包括`BlockingCollection<T>`,`ConcurrentDictionary<T, T>`,`ConcurrentQueue<T>`和`ConcurrentStack<T>`。 泛型編程是一種計算機編程樣式,其中,算法根據待指定的后來的類型編寫,然后在需要作為參數提供的特定類型時實例化。 這種方法由 Ada 于 1983 年率先提出,它允許編寫僅在使用時所使用的類型集不同的常見功能或類型,從而減少了重復。 (維基百科) ## C# `ArrayList` `ArrayList`是標準`System.Collections`命名空間的集合。 它是一個動態數組。 它提供對元素的隨機訪問。 添加數據后,`ArrayList`會自動擴展。 與數組不同,`ArrayList`可以保存多種數據類型的數據。 `ArrayList`中的元素通過整數索引訪問。 索引從零開始。 `ArrayList`末尾的元素索引以及插入和刪除操作需要花費固定的時間。 在動態數組的中間插入或刪除元素的成本更高。 這需要線性時間。 `Program.cs` ```cs using System; using System.Collections; namespace ArrayListEx { class Empty { } class Program { static void Main(string[] args) { var data = new ArrayList(); data.Add("Visual Basic"); data.Add(344); data.Add(55); data.Add(new Empty()); data.Remove(55); foreach (object el in data) { Console.WriteLine(el); } } } } ``` 在上面的示例中,我們創建了一個`ArrayList`集合。 我們添加了一些元素。 它們具有各種數據類型,字符串,整數和類對象。 ```cs using System.Collections; ``` 為了使用`ArrayList`集合,我們需要使用`System.Collections`命名空間。 ```cs var data = new ArrayList(); ``` 創建一個`ArrayList`集合。 ```cs data.Add("Visual Basic"); data.Add(344); data.Add(55); data.Add(new Empty()); data.Remove(55); ``` 我們使用`Add()`方法向數組添加四個元素。 ```cs data.Remove(55); ``` 我們使用`Remove()`方法刪除一個元素。 ```cs foreach(object el in data) { Console.WriteLine(el); } ``` 我們遍歷數組并將其元素打印到控制臺。 ```cs $ dotnet run Visual Basic 344 ArrayListEx.Empty ``` 這是示例的輸出。 ## C# `List` `List`是可以通過索引訪問的對象的強類型列表。 可以在`System.Collections.Generic`命名空間下找到。 `Program.cs` ```cs using System; using System.Collections.Generic; namespace ListEx { class Program { static void Main(string[] args) { var langs = new List<string>(); langs.Add("Java"); langs.Add("C#"); langs.Add("C"); langs.Add("C++"); langs.Add("Ruby"); langs.Add("Javascript"); Console.WriteLine(langs.Contains("C#")); Console.WriteLine(langs[1]); Console.WriteLine(langs[2]); langs.Remove("C#"); langs.Remove("C"); Console.WriteLine(langs.Contains("C#")); langs.Insert(4, "Haskell"); langs.Sort(); foreach (string lang in langs) { Console.WriteLine(lang); } } } } ``` 在前面的示例中,我們使用`List`集合。 ```cs using System.Collections.Generic; ``` `List`集合位于`System.Collections.Generic`命名空間中。 ```cs var langs = new List<string>(); ``` 將創建一個通用動態數組。 我們指定將使用在`<>`字符內指定類型的字符串。 ```cs langs.Add("Java"); langs.Add("C#"); langs.Add("C"); ... ``` 我們使用`Add()`方法將元素添加到列表中。 ```cs Console.WriteLine(langs.Contains("C#")); ``` 我們使用`Contains()`方法檢查列表是否包含特定的字符串。 ```cs Console.WriteLine(langs[1]); Console.WriteLine(langs[2]); ``` 我們使用索引符號訪問`List`的第二個和第三個元素。 ```cs langs.Remove("C#"); langs.Remove("C"); ``` 我們從列表中刪除兩個字符串。 ```cs langs.Insert(4, "Haskell"); ``` 我們在特定位置插入一個字符串。 ```cs langs.Sort(); ``` 我們使用`Sort()`方法對元素進行排序。 ```cs $ dotnet run True C# C False C++ Haskell Java Javascript Ruby ``` 這是示例的結果。 ## C# 集合初始值設定項 集合初始化器允許在對象創建期間為`{}`括號指定集合的??元素。 `Program.cs` ```cs using System; using System.Collections.Generic; using System.Linq; namespace CollectionInitializer { class Program { static void Main(string[] args) { var vals = new List<int>() { 1, 2, 3, 4, 5, 6, 7 }; int sum = vals.Sum(); Console.WriteLine(sum); } } } ``` 該示例創建一個列表并打印其總和。 列表的元素在集合初始化器中指定。 ```cs $ dotnet run 28 ``` 這是輸出。 ## C# `SortedList` `SortedList<T, T>`表示已排序的鍵/值對的集合。 `Program.cs` ```cs using System; using System.Collections.Generic; namespace SortedListEx { class Program { static void Main(string[] args) { var sorted = new SortedList<string, int>(); sorted.Add("coins", 3); sorted.Add("books", 41); sorted.Add("spoons", 5); if (sorted.ContainsKey("books")) { Console.WriteLine("There are books in the list"); } foreach (var pair in sorted) { Console.WriteLine(pair); } } } } ``` 該示例使用排序列表來組織項目。 ```cs var sorted = new SortedList<string, int>(); ``` 排序的列表具有字符串鍵和整數值。 ```cs if (sorted.ContainsKey("books")) { Console.WriteLine("There are books in the list"); } ``` 使用`ContainsKey()`,我們檢查集合中是否有書籍。 ```cs foreach (var pair in sorted) { Console.WriteLine(pair); } ``` 使用`foreach`循環,我們遍歷集合并打印其對。 ```cs $ dotnet run There are books in the list [books, 41] [coins, 3] [spoons, 5] ``` This is the output. ## C# `LinkedList` `LinkedList`是 C# 中的通用雙鏈表。 `LinkedList`僅允許順序訪問。 `LinkedList`允許進行固定時間的插入或移除,但只能順序訪問元素。 由于鏈表需要額外的存儲空間以供參考,因此對于諸如字符之類的小型數據項列表來說,它們是不切實際的。 與動態數組不同,可以將任意數量的項目添加到鏈表(當然受內存限制)而無需重新分配,這是一項昂貴的操作。 `Program.cs` ```cs using System; using System.Collections.Generic; namespace LinkedListEx { class Program { static void Main(string[] args) { var nums = new LinkedList<int>(); nums.AddLast(23); nums.AddLast(34); nums.AddLast(33); nums.AddLast(11); nums.AddLast(6); nums.AddFirst(9); nums.AddFirst(7); LinkedListNode<int> node = nums.Find(6); nums.AddBefore(node, 5); foreach (int num in nums) { Console.WriteLine(num); } } } } ``` 這是一個`LinkedList`示例,其中包含一些方法。 ```cs var nums = new LinkedList<int>(); ``` 這是一個整數`LinkedList`。 ```cs nums.AddLast(23); ... nums.AddFirst(7); ``` 我們使用`AddLast()`和`AddFirst()`方法填充鏈表。 ```cs LinkedListNode<int> node = nums.Find(6); nums.AddBefore(node, 5); ``` `LinkedList`由節點組成。 我們找到一個特定的節點,并在其之前添加一個元素。 ```cs foreach(int num in nums) { Console.WriteLine(num); } ``` 我們正在將所有元素打印到控制臺。 ```cs $ dotnet run 7 9 23 34 33 11 5 6 ``` This is the output. ## C# `dictionary` `dictionary`,也稱為關聯數組,是唯一鍵和值的集合,其中每個鍵與一個值相關聯。 檢索和添加值非常快。 字典占用更多內存,因為每個值都有一個鍵。 `Program.cs` ```cs using System; using System.Collections.Generic; namespace DictionaryEx { class Program { static void Main(string[] args) { var domains = new Dictionary<string, string>(); domains.Add("de", "Germany"); domains.Add("sk", "Slovakia"); domains.Add("us", "United States"); domains.Add("ru", "Russia"); domains.Add("hu", "Hungary"); domains.Add("pl", "Poland"); Console.WriteLine(domains["sk"]); Console.WriteLine(domains["de"]); Console.WriteLine("Dictionary has {0} items", domains.Count); Console.WriteLine("Keys of the dictionary:"); var keys = new List<string>(domains.Keys); foreach (string key in keys) { Console.WriteLine("{0}", key); } Console.WriteLine("Values of the dictionary:"); var vals = new List<string>(domains.Values); foreach (string val in vals) { Console.WriteLine("{0}", val); } Console.WriteLine("Keys and values of the dictionary:"); foreach (KeyValuePair<string, string> kvp in domains) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); } } } } ``` 我們有一本字典,用于將域名映射到其國家名稱。 ```cs var domains = new Dictionary<string, string>(); ``` 我們創建一個包含字符串鍵和值的字典。 ```cs domains.Add("de", "Germany"); domains.Add("sk", "Slovakia"); domains.Add("us", "United States"); ... ``` 我們將一些數據添加到字典中。 第一個字符串是鍵。 第二是值。 ```cs Console.WriteLine(domains["sk"]); Console.WriteLine(domains["de"]); ``` 在這里,我們通過它們的鍵檢索兩個值。 ```cs Console.WriteLine("Dictionary has {0} items", domains.Count); ``` 我們通過引用`Count`屬性來打印項目數。 ```cs var keys = new List<string>(domains.Keys); foreach(string key in keys) { Console.WriteLine("{0}", key); } ``` 這些行從字典中檢索所有鍵。 ```cs var vals = new List<string>(domains.Values); foreach(string val in vals) { Console.WriteLine("{0}", val); } ``` 這些行從字典中檢索所有值。 ```cs foreach(KeyValuePair<string, string> kvp in domains) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); } ``` 最后,我們同時打印字典的鍵和值。 ```cs $ dotnet run Slovakia Germany Dictionary has 6 items Keys of the dictionary: de sk us ru hu pl Values of the dictionary: Germany Slovakia United States Russia Hungary Poland Keys and values of the dictionary: Key = de, Value = Germany Key = sk, Value = Slovakia Key = us, Value = United States Key = ru, Value = Russia Key = hu, Value = Hungary Key = pl, Value = Poland ``` 這是示例的輸出。 ## C# `queue` `queue`是先進先出(FIFO)數據結構。 添加到隊列中的第一個元素將是第一個要刪除的元素。 隊列可以用于處理消息出現時的消息,也可以用于消息到達時為客戶服務的消息。 首先服務的是第一個客戶。 `Program.cs` ```cs using System; using System.Collections.Generic; namespace QueueEx { class Program { static void Main(string[] args) { var msgs = new Queue<string>(); msgs.Enqueue("Message 1"); msgs.Enqueue("Message 2"); msgs.Enqueue("Message 3"); msgs.Enqueue("Message 4"); msgs.Enqueue("Message 5"); Console.WriteLine(msgs.Dequeue()); Console.WriteLine(msgs.Peek()); Console.WriteLine(msgs.Peek()); Console.WriteLine(); foreach (string msg in msgs) { Console.WriteLine(msg); } } } } ``` 在我們的示例中,我們有一個包含消息的隊列。 ```cs var msgs = new Queue<string>(); ``` 創建字符串隊列。 ```cs msgs.Enqueue("Message 1"); msgs.Enqueue("Message 2"); ... ``` `Enqueue()`將消息添加到隊列末尾。 ```cs Console.WriteLine(msgs.Dequeue()); ``` `Dequeue()`方法刪除并返回隊列開頭的項目。 ```cs Console.WriteLine(msgs.Peek()); ``` `Peek()`方法從隊列中返回下一項,但不會將其從集合中刪除。 ```cs $ dotnet run Message 1 Message 2 Message 2 Message 2 Message 3 Message 4 Message 5 ``` `Dequeue()`方法從集合中刪除`Message 1`。 `Peek()`方法沒有。 `Message 2`保留在集合中。 ## C# `Stack` 棧是后進先出(LIFO)數據結構。 添加到隊列中的最后一個元素將是第一個要刪除的元素。 C 語言使用棧將本地數據存儲在函數中。 實現計算器時,還將使用該棧。 `Program.cs` ```cs using System; namespace StackEx { class Program { static void Main(string[] args) { var myStack = new Stack<int>(); myStack.Push(1); myStack.Push(4); myStack.Push(3); myStack.Push(6); myStack.Push(4); Console.WriteLine(myStack.Pop()); Console.WriteLine(myStack.Peek()); Console.WriteLine(myStack.Peek()); Console.WriteLine(); foreach (int item in myStack) { Console.WriteLine(item); } } } } ``` 上面有一個簡單的棧示例。 ```cs var myStack = new Stack<int>(); ``` 創建一個`Stack`數據結構。 ```cs myStack.Push(1); myStack.Push(4); ... ``` `Push()`方法將一個項目添加到棧的頂部。 ```cs Console.WriteLine(stc.Pop()); ``` `Pop()`方法從棧頂部刪除并返回該項目。 ```cs Console.WriteLine(myStack.Peek()); ``` `Peek()`方法從棧頂部返回該項目。 它不會刪除它。 ```cs $ dotnet run 4 6 6 6 3 4 1 ``` 這是程序的輸出。 C# 教程的這一部分專門介紹 C# 中的集合。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看