日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

List泛型集合

發布時間:2025/4/5 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 List泛型集合 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1 List泛型集合的使用
      • 1.1 List\簡要介紹
      • 1.2 List\的創建
      • 1.3 List\和Array的互相轉換
      • 1.4 List\刪除元素
      • 1.5 List\的遍歷
      • 1.6 List\的快速查詢
    • 2 List泛型集合的排序
      • 2.1 值類型元素的排序
      • 2.2 類類型元素使用默認比較器進行排序
      • 2.3 類類型元素使用比較器接口進行排序
      • 2.4 其他高級排序方法
    • 3 泛型集合List作為DataGridView數據源展示和動態排序實現
      • 3.1 項目文件和界面UI
      • 3.2 代碼實現

1 List泛型集合的使用

1.1 List<T>簡要介紹

對于集合:定義的時候,無需規定元素的個數。
泛型:表示一種程序特性,也就是我們在定義的時候,無需指定特定的類型,而在使用的時候,我們必須明確類型??梢詰迷诩现?、方法中、類中。表示為<T>。<T>表示泛型,T是Type的簡寫,表示當前不確定具體類型。可以根據用戶的實際需要,確定當前集合需要存放的數據類型,一旦確定不可改變。
要求:添加到集合中的元素類型,必須和泛型集合定義時規定的數據類型完全一致。數據取出后無需強制轉換。

使用前的準備:

  • 引入命名空間:System.Collections.Generic。
  • 確定存儲類型:List students = new List();

1.2 List<T>的創建

  • 使用add方法依次添加。
  • List<Course> courseList = new List<Course>(); courseList.Add(course1); courseList.Add(course2); courseList.Add(course3); courseList.Add(course4); courseList.Add(course5);
  • 使用集合初始化器,將元素一次性的初始化到集合中。
  • List<Course> courseList = new List<Course>() { course1, course2, course3, course4, course5 }; // Course[] courseArray1 = new Course[] { course1, course2, course3, course4, course5 };
  • 把數組中的元素添加到集合中。
  • List<Course> courseListFromArray = new List<Course>(); courseListFromArray.AddRange(courseArray1);

    1.3 List<T>和Array的互相轉換

    //集合轉換到數組 Course[] courseArray2 = courseList.ToArray(); //數組直接轉換到集合 List<Course> courseList3 = courseArray2.ToList();

    1.4 List<T>刪除元素

    courseList.Remove(course3);//必須要掌握 courseList.RemoveAll(c => c.CourseId > 10002);//了解 courseList.RemoveAt(2); courseList.RemoveRange(1, 2);

    1.5 List<T>的遍歷

  • 使用for循環。
  • for (int i = 0; i < courseList.Count; i++) {Console.WriteLine($"{item.CourseId}\t{item.CourseName}\t{item.ClassHour}\t{item.Teacher}"); }
  • 使用foreach。
  • foreach (Course item in courseList) {Console.WriteLine($"{item.CourseId}\t{item.CourseName}\t{item.ClassHour}\t{item.Teacher}"); }

    1.6 List<T>的快速查詢

    集合查詢方法1:

    List<Course> result1 = courseList.FindAll(c => c.CourseId > 10003);

    集合查詢方法2:

    var result2 = from c in courseList where c.CourseId > 10003 select c; var result3 = result2.ToList();

    2 List泛型集合的排序

    2.1 值類型元素的排序

    List<int> ageList = new List<int> { 20, 19, 25, 30, 26 }; ageList.Sort();//默認按照升序排列foreach (int item in ageList) {Console.WriteLine(item); } ageList.Reverse(); //降序foreach (int item in ageList) {Console.WriteLine(item); }

    2.2 類類型元素使用默認比較器進行排序

    我們對需要排序的類實現系統接口IComparable<Course>。

    public class Course : IComparable<Course> {public Course() { }public Course(int courseId, string courseName, int classHour, string teacher){this.CourseId = courseId;this.CourseName = courseName;this.ClassHour = classHour;this.Teacher = teacher;}public int CourseId { get; set; }//課程編號public string CourseName { get; set; }//課程名稱public int ClassHour { get; set; }//課時public string Teacher { get; set; }//主講老師//接口對應的比較方法(這個方法的簽名,千萬不要動)public int CompareTo(Course other){//return this.CourseId.CompareTo(other.CourseId);return other.CourseId.CompareTo(CourseId);//如果把this放到前面,表示按照升序排列,other放到前面就是按照降序排列} }

    2.3 類類型元素使用比較器接口進行排序

    以上我們使用默認比較器進行排序,很不方便,如果我們需要多種排序,怎么辦?比較器接口:其實就是我們可以任意的指定對象屬性排序,從而實現動態排序。

    /// <summary> /// 課程編號升序 /// </summary> class CourseIdASC : IComparer<Course> {public int Compare(Course x, Course y){return x.CourseId.CompareTo(y.CourseId);} }// 使用方式 //排序方法的定義: public void Sort(IComparer<T> comparer); courseList.Sort(new CourseIdASC());

    2.4 其他高級排序方法

    // 使用LINQ實現排序 var list1 = from c in courseList orderby c.CourseId ascending select c; // 使用擴展方法OrderByDescending實現降序 var list2 = courseList.OrderByDescending(c => c.CourseId); // 使用擴展方法OrderBy實現升序 var list3 = courseList.OrderBy(c => c.ClassHour);

    3 泛型集合List作為DataGridView數據源展示和動態排序實現

    3.1 項目文件和界面UI

    項目文件如下:

    界面UI:

    對于界面中的DataGridView需要注意,啟用添加、啟用編輯、啟用刪除、啟用列重新排序全部不要勾選。

    3.2 代碼實現

    FrmMain.cs:

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace ListOrder_20191217 {public partial class FrmMain : Form{List<Student> list = null;public FrmMain(){InitializeComponent();InitList();}private void InitList(){Student student1 = new Student() { Name = "Lili", Age = 20, Id = "1000", Addr = "廣東省廣州市" };Student student2 = new Student() { Name = "Mary", Age = 22, Id = "1004", Addr = "北京市" };Student student3 = new Student() { Name = "Yang", Age = 25, Id = "1001", Addr = "湖南省長沙市" };Student student4 = new Student() { Name = "LangLang", Age = 23, Id = "1003", Addr = "湖北省武漢市" };Student student5 = new Student() { Name = "Chi", Age = 28, Id = "1002", Addr = "廣西省桂林市" };list = new List<Student>() { student1, student2, student3, student4, student5 };}private void btnShowStuInfo_Click(object sender, EventArgs e){dgvStuInfo.DataSource = list;//dgvStuInfo.AutoResizeColumns();}private void btnASCOrderById_Click(object sender, EventArgs e){dgvStuInfo.DataSource = list;list.Sort(new IdASC());dgvStuInfo.Refresh();}private void btnDESCOrderById_Click(object sender, EventArgs e){dgvStuInfo.DataSource = list;list.Sort(new IdDESC());dgvStuInfo.Refresh();}} }

    Student.cs:

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace ListOrder_20191217 {public class Student{public string Name { get; set; }public int Age { get; set; }public string Id { get; set; }public string Addr { get; set; }}/// <summary>/// 學號升序排列/// </summary>public class IdASC : IComparer<Student>{public int Compare(Student x, Student y){return x.Id.CompareTo(y.Id);}}/// <summary>/// 學號降序排列/// </summary>public class IdDESC : IComparer<Student>{public int Compare(Student x, Student y){return y.Id.CompareTo(x.Id);}} }

    參考資料:

  • .NET/C#工控上位機VIP系統學習班【喜科堂互聯教育】
  • 總結

    以上是生活随笔為你收集整理的List泛型集合的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。