C# 排序列表

WBOY
发布: 2024-09-03 15:24:03
原创
210 人浏览过

按键排序的键值对的集合,在C#中称为SortedList,默认按升序排序,该集合有泛型集合和非泛型集合System.Collections.Generic 命名空间定义通用排序列表和 System. Collections 命名空间定义了非泛型排序列表,IEnumerable、ICollection、IDictionary 和 IClonable 接口由排序列表类实现,排序列表中的任何元素都由其索引或键标识,排序列表的对象内部维护两个数组存储列表的元素,其中一个数组用于存储键,另一个数组用于存储与键关联的值。

语法:

SortedListlist_name = new SortedList();
登录后复制

其中 list_name 是列表的名称。

C# 中 SortedList 的工作

  • 默认根据键升序排序并由键或索引标识的键和值对的集合,在 C# 中称为 SortedList。
  • SortedList 内部维护了两个数组,一个数组用于存储键,另一个数组用于存储与键关联的值。
  • 键永远不能为空,而值可以为空。
  • SortedList 持有的元素数量就是 SortedList 的容量。
  • SortedList 中不允许有重复的键。
  • 由于SortedList的元素会被排序,所以对SortedList执行的操作会比较慢。
  • 整数索引可用于访问集合中的元素。

C# SortedList 示例

下面是 SortedList C# 的示例:

示例#1

C# 程序创建一个 SortedList 并检查 SortedList 的对象是否具有固定大小或没有,以及检查 SortedList 是否只读。

代码:

using System;
using System.Collections;
//a class called program is created
class program
{
// main method is called
public static void Main()
{
// a sorted list is created
SortedList List = new SortedList();
//Adding the keys and values pairs to the created sorted list
List.Add("10", "Shobha");
List.Add("20", "Ramya");
List.Add("30", "Rahul");
List.Add("40", "Bhuvan");
List.Add("50", "Kiran");
//Displaying the elements of the sorted list by using keys
for (int r = 0; r <List.Count; r++)
{
Console.WriteLine("{0} and {1}",
List.GetKey(r),
List.GetByIndex(r));
}
// checking if the sorted list has a fixed size or no
Console.WriteLine(List.IsFixedSize);
//checking if the sorted list is read only or not
Console.WriteLine(List.IsReadOnly);
}
}
登录后复制

输出:

C# 排序列表

说明:在上面的程序中,创建了一个名为program的类。然后调用main方法。然后创建一个新的排序列表。然后元素以键和值对的形式添加到新创建的排序列表中。然后使用键显示排序列表的元素。然后通过使用排序列表的属性,检查排序列表是否具有固定大小。然后通过使用排序列表的属性,检查排序列表是否是只读的。程序的输出如上面的快照所示。

示例#2

C# 程序创建排序列表并显示排序列表中的元素数量、显示排序列表的容量以及清除排序列表中的所有元素。

代码:

using System;
using System.Collections;
//a class called program is created
class program
{
// main method is called
public static void Main()
{
//a sorted list is created
SortedList List = new SortedList();
// Adding elements to SortedList
List.Add("10", "Shobha");
List.Add("20", "Ramya");
List.Add("30", "Rahul");
List.Add("40", "Bhuvan");
List.Add("50", "Kiran");
//the number of elements in the newly created sorted list is displayed
Console.WriteLine("The count of the elements in the sorted list is : "
+ List.Count);
//the capacity of the newly created sorted list is displayed
Console.WriteLine("The newly created sorted list's capacity is : "
+ List.Capacity);
//Deleting all the elements from the newly created sorted list
List.Clear();
// the number of elements in the sorted list after using clear() function is displayed
Console.WriteLine("The count of the elements in the sorted list after using the clear() function is : "
+ List.Count);
// the capacity of the sorted list after using the clear() function is displayed
Console.WriteLine("The sorted list's capacity after using the clear() function is : "
+ List.Capacity);
}
}
登录后复制

输出:

C# 排序列表

说明:在上面的程序中,创建了一个名为program的类。然后调用main方法。然后创建一个新的排序列表。然后元素以键和值对的形式添加到新创建的排序列表中。然后使用 count 属性显示排序列表的元素计数。然后通过使用排序列表的容量属性,检查排序列表的容量。然后利用排序列表的clear属性,删除排序列表中的元素。然后再次使用 count 属性显示排序列表的元素计数。然后再次使用排序列表的容量属性,检查排序列表的容量。程序的输出如上面的快照所示。

优点

在 C# 中使用 SortedList 有几个优点。他们是:

  • SortedList 不允许重复键。
  • 由于非泛型集合,相同类型的值和不同类型的值可以存储在 SortedList 中。
  • SortedList 的键值对可以转换为 Dictionary 条目。
  • IEnumerable、Icollection、Iclonable 和 Dictionary 接口由 SortedList 类实现。

结论:在本教程中,我们通过定义了解 C# 中 SortedList 的概念、C# 中 SortedList 的语法、通过示例了解 C# 中 SortedList 的工作原理及其输出以及在 C# 中使用 SortedList 的优点.

推荐文章

这是 C# SortedList 的指南。在这里,我们讨论 C# SortedList 简介及其优点以及示例和代码实现。您还可以浏览我们其他推荐的文章以了解更多信息 –

  1. C# 中的随机数生成器是什么?
  2. Java 中的静态构造函数 |工作|应用
  3. C# 中的 TextWriter |示例
  4. 如何在 C# 中使用静态构造函数?

以上是C# 排序列表的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!