C# 中的列表

王林
發布: 2024-09-03 15:28:00
原創
311 人瀏覽過

C#中的清單在資料儲存和檢索中扮演著非常重要的角色。以下是 C# 中通用列表 ( List ) 的一些要點:

  • 列表
  • 它位於收藏夾下。通用命名空間。
  • 清單中的元素可以透過其索引號碼來訪問,清單中的索引從零開始。
  • 清單可以動態調整大小。
  • 如果清單的元素是引用類型,那麼清單也可以接受空值。
  • 它允許元素重複。

文法:

List<T> list_name = new List<T>();
登入後複製

說明:上述語句中List

我們也可以藉助 IList

IList<T> list_name = new List<T>();
登入後複製

如何用 C# 建立清單?

為了與 List 合作,首先,我們需要在程式中匯入System.Collections.Generic命名空間。

C# 中建立清單的方法有很多種,例如:

  • 使用 List

範例:

List<int> lstNum = new List<int>();
登入後複製

上面的語句將會建立一個具有預設容量的整數清單。如果使用者未定義清單的容量,則每次將項目新增至清單時,清單的大小都會增加。

ASP.NET 培訓(9 門課程,19 個項目).NET 培訓計劃(5 門課程,19 個項目)

  • 我們可以使用 Add() 方法將項目新增至清單。

範例:

lstNum.Add(1);
lstNum.Add(2);
lstNum.Add(3);
登入後複製

建立一個由使用者定義容量的清單。

範例:

List<string> lstString = new List<string>(3);
登入後複製

上面的語句將會建立一個容量為 3 的字串清單。當清單中新增三個以上元素時,容量會自動擴展。我們也可以在初始化清單時將項目新增到清單中。

List<string> lstString = new List<string>(3)
{
"Neha",
"Shweta",
"Megha"
};
登入後複製

我們也可以藉助另一個元素集合來建立清單。

範例:

//string array of names
string[] names = {"Neha", "Shweta", "Megha"};
//creating list by using string array
List<string> lstNames = new List<string>(names);
登入後複製

我們可以使用 AddRange() 方法將另一個元素集合加入到列表中。

範例:

string[] names = {"Neha", "Shweta", "Megha"};
List<string> lstNames = new List<string>();
//adding elements of string array to list
lstNames.AddRange(names);
登入後複製

C# 中的 List 方法及範例

讓我們討論List類別的一些重要方法如下:

1.加(T)

此方法用於在清單末尾新增一個物件。它可以為引用類型添加空值。

範例:

using System;
using System.Collections.Generic;
public class ListDemo
{
public static void Main()
{
List<int> lstNum = new List<int>(){1, 2, 3, 4};
//Adding 5 at the end of list
lstNum.Add(5);
foreach(int num in lstNum)
{
Console.WriteLine(num);
}
}
}
登入後複製

輸出:

C# 中的列表

2.清除()

此方法用於刪除清單中的所有元素。

範例:

using System;
using System.Collections.Generic;
public class ListDemo
{
public static void Main()
{
List<int> lstNum = new List<int>(){1, 2, 3, 4, 5};
//removing all elements from the list
lstNum.Clear();
if(lstNum.Count > 0)
Console.WriteLine("List is not empty");
else
Console.WriteLine("List is empty");
}
}
登入後複製

輸出:

C# 中的列表

3.插入(Int32,T)

此方法用於在清單中的指定位置插入一個元素。它有兩個參數,第一個參數是要插入元素的索引號,第二個參數是元素本身。

範例:

using System;
using System.Collections.Generic;
public class ListDemo
{
public static void Main()
{
List<string> lstCities = new List<string>(){"Mumbai", "Pune", "Bengaluru"};
//inserting element at third position
lstCities.Insert(2, "Chennai");
foreach(string city in lstCities)
{
Console.WriteLine(city);
}
}
}
登入後複製

輸出:

C# 中的列表

4.移除At( Int32 )

此方法用於從清單中刪除指定位置的項目。

範例:

using System;
using System.Collections.Generic;
public class ListDemo
{
public static void Main()
{
List<string> lstCities = new List<string>() {"Mumbai","Pune","Bengaluru"};
Console.WriteLine("Initial list values");
foreach(string city in lstCities)
{
Console.WriteLine(city);
}
//removing element at second position
lstCities.RemoveAt(1);
Console.WriteLine("\nAfter removing element at second position");
foreach(string city in lstCities)
{
Console.WriteLine(city);
}
}
}
登入後複製

輸出:

C# 中的列表

5.排序()

此方法用於使用預設比較器對清單的元素進行排序。

範例:

using System;
using System.Collections.Generic;
public class ListDemo
{
public static void Main()
{
List<string> lstCities = new List<string>(){"Mumbai","Pune","Bengaluru"};
Console.WriteLine("Initial list values");
foreach(string city in lstCities)
{
Console.WriteLine(city);
}
//sorting elements in ascending order
lstCities.Sort();
Console.WriteLine("\nList after sorting in ascending order");
foreach(string city in lstCities)
{
Console.WriteLine(city);
}
//sorting elements in descending order by calling Reverse()
lstCities.Reverse();
Console.WriteLine("\nList after sorting in descending order");
foreach(string city in lstCities)
{
Console.WriteLine(city);
}
}
}
登入後複製

輸出:

C# 中的列表

在上面的程式中,首先,我們使用 Sort() 對清單進行升序排序。現在,為了按降序對清單進行排序,我們對已排序的清單呼叫 Reverse() 方法。我們可以使用 Sort() 方法對 int、string 等類型的列表進行排序,但要對自訂物件類型的列表進行排序,我們需要實作 IComparable 接口,或者也可以使用 LINQ。我們可以用另一種方​​式對這種類型的列表進行排序,如下例所示:

範例:

using System;
using System.Collections.Generic;
public class Student
{
public string Name { get; set; }
public int Marks { get; set; }
public Student(string name, int marks)
{
Name = name;
Marks = marks;
}
}
public class ListDemo
{
public static void Main()
{
List<Student> lstStudents = new List<Student>();
lstStudents.Add(new Student("Neha", 90));
lstStudents.Add(new Student("John", 75));
lstStudents.Add(new Student("Kate", 88));
lstStudents.Add(new Student("Arya", 70));
//sorting students in ascending order of their marks
lstStudents.Sort(CompareMarks);
foreach (Student student in lstStudents)
{
Console.WriteLine(student.Name + ": " + student.Marks);
}
}
public static int CompareMarks(Student student1, Student student2)
{
return student1.Marks.CompareTo(student2.Marks);
}
}
登入後複製

輸出:

C# 中的列表

結論

列表

以上是C# 中的列表的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!