C# SortedList用法示例介绍

2021年5月8日16:19:48 发表评论 931 次浏览

C#中, SortedList是键/值对的集合, 这些键/值对根据键进行排序。默认情况下, 此集合按升序对键/值对进行排序。它具有通用和非通用类型的集合。通用SortedList定义在System.Collections.Generic命名空间, 而非通用SortedList在下面定义系统集合命名空间, 这里我们将讨论非通用类型的SortedList。

重要事项:

  • SortedList类实现IEnumerable, ICollection, 词典和可克隆接口。
  • 在SortedList中, 可以通过元素的键或索引来访问元素。
  • SortedList对象在内部维护两个数组来存储列表的元素, 即, 一个数组用于键, 另一个数组用于关联的值。
  • 在此, 键不能为null, 但值可以为null。
  • SortedList对象的容量是它可以容纳的键/值对的数量。
  • 在SortedList中, 不允许重复的键。
  • 在SortedList中, 由于非通用集合, 你可以存储相同类型和不同类型的值。如果在程序中使用通用SortedList, 则值的类型必须相同。
  • 在SortedList中, 你不能将不同数据类型的键存储在同一SortedList中, 因为编译器将引发异常。因此, 始终将键添加到相同类型的SortedList中。
  • 你也可以将SortedList的键/值对转换为DictionaryEntry。

如何创建一个SortedList?

SortedList类提供6用于创建SortedList的不同类型的构造函数, 这里我们仅使用SortedList()构造函数。要了解有关SortedList的构造函数的更多信息, 请参阅C#| SortedList类.

SortedList():它用于创建SortedList类的实例, 该实例为空, 具有默认的初始容量, 并根据添加到SortedList对象的每个键实现的IComparable接口进行排序。

第1步:借助using关键字, 在程序中包含System.Collections命名空间:

using System.Collections;

第2步:使用SortedList类创建一个SortedList, 如下所示:

SortedList list_name = new SortedList();

第三步:如果要在SortedList中添加键/值对, 请使用Add()方法在你的SortedList中添加键/值对。而且, 你也可以在不使用Add()方法的情况下将键/值对存储在SortedList中, 这种语法称为集合初始化语法, 如以下示例所示。

步骤4:使用以下命令访问SortedList的键/值对三不同的方式:

for循环:

你可以使用for循环访问SortedList的键/值对。

例子:

for ( int x = 0; x <my_slist1.Count; x++)
{
     Console.WriteLine( "{0} and {1}" , my_slist1.GetKey(x), my_slist1.GetByIndex(x));
}

使用索引:

你可以使用索引访问SortedList的各个值。你需要将键或索引作为参数传递, 以找到相应的值。如果指定的键不可用, 则编译器将引发错误。

例子:

Console.WriteLine( "Value is:{0}" , my_slist1[1.04]);
  
string x = ( string )my_slist[1.02];
  
Console.WriteLine(x);

foreach循环:

你可以使用foreach循环来访问SortedList的键/值对。

例子:

foreach (DictionaryEntry pair in my_slist1)
{
     Console.WriteLine( "{0} and {1}" , pair.Key, pair.Value);
}

例子:

//C# program to illustrate how
//to create a sortedlist
using System;
using System.Collections;
  
class GFG {
  
     //Main Method
     static public void Main()
     {
  
         //Creating a sortedlist
         //Using SortedList class
         SortedList my_slist1 = new SortedList();
  
         //Adding key/value pairs in 
         //SortedList using Add() method
         my_slist1.Add(1.02, "This" );
         my_slist1.Add(1.07, "Is" );
         my_slist1.Add(1.04, "SortedList" );
         my_slist1.Add(1.01, "Tutorial" );
  
         foreach (DictionaryEntry pair in my_slist1)
         {
             Console.WriteLine( "{0} and {1}" , pair.Key, pair.Value);
         }
         Console.WriteLine();
  
         //Creating another SortedList
         //using Object Initializer Syntax
         //to initalize sortedlist
         SortedList my_slist2 = new SortedList() {
                                   { "b.09" , 234 }, { "b.11" , 395 }, { "b.01" , 405 }, { "b.67" , 100 }};
                  
         foreach (DictionaryEntry pair in my_slist2)
         {
             Console.WriteLine( "{0} and {1}" , pair.Key, pair.Value);
         }
     }
}

输出如下:

1.01 and Tutorial
1.02 and This
1.04 and SortedList
1.07 and Is

b.01 and 405
b.09 and 234
b.11 and 395
b.67 and 100

如何从SortedList中删除元素?

  • 明确:此方法用于从SortedList对象中删除所有元素。
  • 去掉:此方法用于从SortedList对象中删除具有指定键的元素。
  • RemoveAt:此方法用于删除SortedList对象指定索引处的元素。

例子:

//C# program to illustrate how to
//remove key/value pairs from 
//the sortedlist
using System;
using System.Collections;
  
class GFG {
  
     //Main Method
     static public void Main()
     {
  
         //Creating a sortedlist
         //Using SortedList class
         SortedList my_slist = new SortedList();
  
         //Adding key/value pairs in SortedList
         //Using Add() method
         my_slist.Add(1.02, "This" );
         my_slist.Add(1.07, "Is" );
         my_slist.Add(1.04, "SortedList" );
         my_slist.Add(1.01, "Tutorial" );
  
         foreach (DictionaryEntry pair in my_slist)
         {
             Console.WriteLine( "{0} and {1}" , pair.Key, pair.Value);
         }
         Console.WriteLine();
  
         //Remove value having 1.07 key
         //Using Remove() method
         my_slist.Remove(1.07);
  
         //After Remove() method
         foreach (DictionaryEntry pair in my_slist)
         {
             Console.WriteLine( "{0} and {1}" , pair.Key, pair.Value);
         }
         Console.WriteLine();
  
         //Remove element at index 2
         //Using RemoveAt() method
         my_slist.RemoveAt(2);
  
         //After RemoveAt() method
         foreach (DictionaryEntry pair in my_slist)
         {
             Console.WriteLine( "{0} and {1}" , pair.Key, pair.Value);
         }
         Console.WriteLine();
  
         //Remove all key/value pairs
         //Using Clear method
         my_slist.Clear();
         Console.WriteLine( "The total number of key/value pairs" +
                     " present in my_slist:{0}" , my_slist.Count);
     }
}

输出如下:

1.01 and Tutorial
1.02 and This
1.04 and SortedList
1.07 and Is

1.01 and Tutorial
1.02 and This
1.04 and SortedList

1.01 and Tutorial
1.02 and This

The total number of key/value pairs present in my_slist:0

如何在SortedList中检查键/值对的可用性?

在SortedList中, 可以使用以下方法检查给定对是否存在:

  • 包含:此方法用于检查SortedList对象是否包含特定键。
  • ContainsKey:此方法用于检查SortedList对象是否包含特定键。
  • 包含值:此方法用于检查SortedList对象是否包含特定值。

例子:

//C# program to illustrate how to
//check the given key or value 
//present in the sortedlist or not
using System;
using System.Collections;
  
class GFG {
  
     //Main Method
     static public void Main()
     {
  
         //Creating a sortedlist
         //Using SortedList class
         SortedList my_slist = new SortedList();
  
         //Adding key/value pairs in 
         //SortedList using Add() method
         my_slist.Add(1.02, "This" );
         my_slist.Add(1.07, "Is" );
         my_slist.Add(1.04, "SortedList" );
         my_slist.Add(1.01, "Tutorial" );
  
         //Using Contains() method to check
         //the specified key is present or not
         if (my_slist.Contains(1.02) == true ) 
         {
             Console.WriteLine( "Key is found...!!" );
         }
  
         else 
         {
             Console.WriteLine( "Key is not found...!!" );
         }
  
         //Using ContainsKey() method to check
         //the specified key is present or not
         if (my_slist.ContainsKey(1.03) == true ) 
         {
             Console.WriteLine( "Key is found...!!" );
         }
         else
         {
             Console.WriteLine( "Key is not found...!!" );
         }
  
         //Using ContainsValue() method to check
         //the specified value is present or not
         if (my_slist.ContainsValue( "Is" ) == true ) 
         {
             Console.WriteLine( "Value is found...!!" );
         }
  
         else 
         {
             Console.WriteLine( "Value is not found...!!" );
         }
     }
}

输出如下:

Key is found...!!
Key is not found...!!
Value is found...!!

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: