集合

列表

预计阅读时间2 分钟 7 views

C# 列表

C# List

前言

List<T> 是一个类,它包含多个相同数据类型的对象,并且可以通过索引进行访问。例如:

// 包含整数值的列表 
List<int> number = new List<int>() { 1, 2, 3 };

在这里,number 是一个包含整数值(1、2 和 3)的 List

作用

List<T> 类提供了一种动态大小的集合实现,可以存储和管理一组相同类型的对象。与数组不同,List<T> 可以在运行时动态增加或减少元素的数量,使得对数据的管理更加灵活和高效。

使用场景

List<T> 适用于以下场景:

  1. 需要动态调整大小的集合:例如需要在程序运行时不断添加或删除元素。
  2. 对集合中的元素进行排序和检索:例如在列表中查找特定元素或对列表进行排序。
  3. 集合元素的频繁修改:如在游戏中处理玩家列表,或在应用程序中处理任务列表。

示例

  1. 创建 List
using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // 创建一个名为 subjects 的列表,包含两个元素
        List<string> subjects = new List<string>() { "英语", "数学" };
    }
}

在这个例子中,我们创建了一个名为 subjectsList,它包含两个字符串元素:”英语” 和 “数学”。

  1. 访问 List 元素
using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // 创建一个列表
        List<string> languages = new List<string>() { "Python", "Java" };

        // 访问列表中的第一个和第二个元素
        Console.WriteLine("列表的第一个元素是 " + languages[0]);
        Console.WriteLine("列表的第二个元素是 " + languages[1]);
    }
}

输出:

列表的第一个元素是 Python
列表的第二个元素是 Java

在这个例子中,我们使用索引访问列表中的元素。由于列表的索引从0开始,所以 languages[0] 访问第一个元素,languages[1] 访问第二个元素。

  1. 遍历 List
using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // 创建一个列表
        List<string> albums = new List<string>() { "红", "午夜", "名誉" };

        // 遍历列表中的所有元素
        for (int i = 0; i < albums.Count; i++)
            Console.WriteLine(albums[i]);
    }
}

输出:

红
午夜
名誉

在这个例子中,我们使用 for 循环遍历 albums 列表中的所有元素。Count 属性返回列表中的元素总数。

  1. 在 List 中添加元素
using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // 创建一个列表
        List<string> country = new List<string>() { "俄罗斯" };

        // 向 country 列表中添加 "中国"
        country.Add("中国");

        // 向 country 列表中添加 "美国"
        country.Add("美国");

        // 遍历并打印 country 列表中的所有元素
        for (int i = 0; i < country.Count; i++)
            Console.WriteLine(country[i]);
    }
}

输出:

俄罗斯
中国
美国

在这个例子中,我们首先创建了一个包含 “俄罗斯” 的列表,然后使用 Add() 方法将 “美国” 和 “日本” 添加到列表中。

  1. 在 List 中插入元素
using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        // 创建一个列表
        List<string> languages = new List<string>() { "Python", "Java", "C" };

        // 在索引2的位置插入 "JavaScript"
        languages.Insert(2, "JavaScript");

        // 显示索引2处的元素
        Console.WriteLine(languages[2]);
    }
}

输出:

JavaScript

在这个例子中,Insert(2, "JavaScript") 方法在列表的第二个索引位置插入了 “JavaScript”。

  1. 从 List 中删除元素
using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        var car = new List<string>() { "宝马", "特斯拉", "铃木", "特斯拉" };

        // 删除列表中第一个出现的 "特斯拉"
        car.Remove("特斯拉");

        // 删除第一个出现的 "铃木"
        car.Remove("铃木");

        // 打印删除后的列表
        for (int i = 0; i < car.Count; i++)
        {
            Console.WriteLine(car[i]);
        }
    }
}

输出:

宝马
特斯拉

在这个例子中,Remove("特斯拉") 方法删除了列表中第一个出现的 “特斯拉”,Remove("铃木") 删除了第一个出现的 “铃木”。

  1. 使用 RemoveAt() 方法删除元素
using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        var car = new List<string>() { "宝马", "特斯拉", "铃木", "特斯拉" };

        // 删除索引为2的位置的元素
        car.RemoveAt(2);

        // 打印删除后的列表
        for (int i = 0; i < car.Count; i++)
        {
            Console.WriteLine(car[i]);
        }
    }
}

输出:

宝马
特斯拉
特斯拉

在这个例子中,RemoveAt(2) 方法删除了列表中索引为2的位置的元素 “铃木”。

结语

List<T> 是 C# 中一个非常有用的集合类,允许我们动态地管理相同类型的对象。通过 List<T>,我们可以方便地执行各种操作,如添加、删除、插入和遍历元素。掌握 List<T> 的基本用法可以帮助我们在编程中更高效地处理和操作数据。

Leave a Comment

分享此文档

列表

或复制链接

内容