C# 有序列表

SortedList 是一个非泛型集合,包含按顺序排序的键/值对。例如:

using System;
using System.Collections;

class Program
{
    public static void Main()
    {
        // create a SortedList
        SortedList myList = new SortedList();
        myList.Add(2, "Python");
        myList.Add(1, "Java");
        myList.Add(3, "C");

        // iterate through myList 
        for (int i = 0; i < myList.Count; i++)
        {
            Console.WriteLine("{0} : {1} ", myList.GetKey(i),
            myList.GetByIndex(i));
        }
    }
}

输出

1 : Java 
2 : Python 
3 : C 

这里,myList 是一个包含键/值对的 SortedList

我们将详细了解 SortedList


创建 SortedList

要在 C# 中创建 SortedList,我们需要使用 System.Collections 命名空间。以下是如何创建 SortedList 的方法:

// create a sorted list 
SortedList myList = new SortedList();

在这里,我们创建了一个名为 myListSortedList


SortedList 的基本操作

在 C# 中,我们可以对 sortedlist 执行不同的操作。在本教程中,我们将介绍一些常用的 SortedList 操作。

  • 添加元素
  • 访问元素
  • 更改元素
  • 移除元素

让我们更详细地看看如何执行这些操作!


向 SortedList 添加元素

C# 提供了一个名为 Add() 的方法,我们可以使用它向 SortedList 添加元素。例如:

using System;
using System.Collections;

class Program
{
    public static void Main()
    {
        // create a SortedList and add items 
        SortedList person = new SortedList();
 
        person.Add(2, 45);
        person.Add(1, "Jack");
        person.Add(3, "Florida");
    }
}

在上面的示例中,我们创建了一个名为 personSortedList

然后,我们添加了三个值,一个整数类型(**45**)和两个字符串类型("Jack""Florida"),以及它们的键(**2**、**1** 和 **3**)。


访问 SortedList

我们可以通过键来访问 SortedList 中的元素。例如:

using System;
using System.Collections;

class Program
{
    public static void Main()
    {
        SortedList myList = new SortedList();
        myList.Add(2, "Python");
        myList.Add(1, "Java");
        myList.Add(3, "C");

// access the element whose key is 2 Console.WriteLine("Element whose key is 2: " + myList[2]);
// access the element whose key is 1 Console.WriteLine("Element whose key is 1: " + myList[1]);
} }

输出

Element whose key is 2: Python
Element whose key is 1: Java

在上面的示例中,我们使用了键来访问元素。

  • myList[2] - 访问键为 **2** 的元素
  • myList[1] - 访问键为 **1** 的元素

注意: 访问时,如果我们传入不存在的键,编译器会报错。


遍历 SortedList

在 C# 中,我们也可以使用 for 循环遍历 SortedList 的每个元素。例如:

using System;
using System.Collections;

class Program
{
    public static void Main()
    {
        SortedList myList = new SortedList();
        myList.Add(2, "BMW");
        myList.Add(1, 96);
        myList.Add(3, "Pizza");

// iterate through myList for (int i = 0; i < myList.Count; i++) { Console.WriteLine("{0} : {1} ", myList.GetKey(i), myList.GetByIndex(i));
} } }

输出

1 : 96 
2 : BMW 
3 : Pizza

在上面的示例中,我们使用了 for 循环来遍历 myList

为了获取 SortedList 的键和值,我们分别使用 GetKey()GetByIndex() 方法。

由于 SortedList 按升序排列键,因此输出的键是有序的。

注意: Count 属性计算列表中元素的总数。


移除 SortedList 元素

我们可以使用 **2** 种方法从 SortedList 中删除一个或多个项:

  • Remove(key) - 根据指定的键移除元素
  • RemoveAt(index) - 根据指定的索引移除元素

让我们通过这两种方法来看示例。


示例:Remove() 方法

using System;
using System.Collections;

class Program {
	public static void Main() {
	    SortedList myList = new SortedList();
	    myList.Add(2, "Evermore");
	    myList.Add(1, "Reputation");
	    myList.Add(3, "Folklore");
	    
	
// remove element whose key is 1 i.e "Reputation" myList.Remove(1);
// iterating through the modified SortedList for (int i =0; i< myList.Count; i++) { Console.WriteLine("{0} : {1} ", myList.GetKey(i), myList.GetByIndex(i)); } } }

输出

2 : Evermore 
3 : Folklore 

在上面的示例中,我们移除了键为 **1** 的元素。

这里,myList.Remove(1)"Reputation"myList 中移除。因此,当我们遍历 myList 时,会得到一个修改后的列表作为输出。


示例:RemoveAt() 方法

using System;
using System.Collections;

class Program {
	public static void Main() {
	    SortedList myList = new SortedList();
	    myList.Add(2, "Evermore");
	    myList.Add(1, "Reputation");
	    myList.Add(3, "Folklore");
	    
	
// remove element which is present in index 1 i.e "Evermore" myList.RemoveAt(1);
// iterating through the modified SortedList for (int i =0; i< myList.Count; i++) { Console.WriteLine("{0} : {1} ", myList.GetKey(i), myList.GetByIndex(i)); } } }

输出

1 : Reputation  
3 : Folklore 
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战