C# 基础15 哈希表 排序列表

本文介绍了C#中的哈希表(Hashtable)和排序列表(SortedList)的基础知识,包括它们的特性、常用属性及操作方法。哈希表允许通过键访问元素,而排序列表则是数组和哈希表的结合,支持键和索引访问,并保持键的排序。文章详细讲解了如何添加、判断、清除元素以及调整容量等操作。
摘要由CSDN通过智能技术生成

哈希表(Hashtable):使用键来访问集合中的元素

排序列表(SortedList):代表了一系列按照键来排序的键/值对,这些键值对可以通过键和索引来访问

排序列表是数组和哈希表的组合,它包含一个可使用键或索引访问各项的列表,如果您使用索引访问各项,则它是一个动态数组(ArrayList),如果您使用键访问各项,则它是一个哈希表(Hashtable),集合中的各项总是按键值排序。

堆栈(Stack):代表了一个后进先出的对象集合,当需要对各项进行后进先出的访问时,则使用堆栈

在列表中添加一项,成为推入元素,从列表中移除一项时,称为弹出元素。

队列(Queue):代表了一个先进先出的对象集合。当需要对各项进行先进先出的访问时,则使用队列。

在列表中添加一项,称为入队,当从列表中移除一项时,称为出队。


一、语法格式:

先添加命名空间 using System.Collections;

使用默认的初始容量,Hashtable使用加载因子、Queue队列使用增长因子Hashtable 标识符=new Hashtable();SortedList 标识符=new SortedList ();Stack标识符=new Stack();Queue 标识符=new Queue();
使用指定的初始容量,默认加载因子Hashtable 标识符=new Hashatable(指定的初始容量);SortedList 标识符=new SortedList(5);Stack 标识符=new Stack(制定的初始容量);Queue 标识符=new Queue(指定的容量);
复制对象拥有相同的初始容量并使用默认的加载因子,Stack从集合复制元素并具有相同的初始容量Hashtable 标识符=new Hashtable(其元素被复制到新哈希表的原表);SortedList 标识符=new SortedList(制定的要复制元素的字典);Stack 标识符=new Stack(被复制元素的源集合)Queue 标识符=new Queue(元素被复制的源集合);
X:0.1-1.0范围内的数字,再乘以提供最佳性能的默认值。结果是元素与存储桶的最大比率Hashtable 标识符=new Hashtable(指定的初始容量,X); 具有制定的初始容量并使用指定的增长因子Queue 标识符=new Queue(可包含的初始元素数,因子的容量)

二、Hashtable类和SortedList类,哈希表与排序列表的一些常用属性

Public virtual bool IsReadOnly{get;}

摘要:获取一个值,该值指示System.Collections.Hashtable/SortedList是否为只读。

返回:bool值;如果System.Collections.哈希表/排序列表 是只读的,输出结果为true;否则为False;

判断是否只读。

代码:

Hashtable hashtable = new Hashtable();         //哈希表

Console.WriteLine(hashtable.IsReadOnly);
 
--->
False
----------------------------------------------
SortedList sortedList = new SortedList();      //排序列表

Console.WriteLine(sortedList.IsReadOnly);

--->
False

Public virtual int IsFixedSize{get;}

 摘要:获取包含在System.Collections.Hashtable是否具有固定大小。

            获取包含在System.Collections.SortedList中对象具有固定的大小。

返回:包含在System.Collections.Hashtable是否具有固定大小,有返回true默认返回false

           包含在System.Collections.SortedList对象具有固定大小,有返回true默认返回false

获取Hashtable中包含键值对的个数/判断是否具有固定大小

Hashtable hashtable = new Hashtable();                 //哈希表

Console.WriteLine(hashtable.IsFixedSize);
 
--->
False
-------------------------------------------------------------
SortedList sortedList = new SortedList();              //排序列表

Console.WriteLine(sortedList.IsFixedSize);

--->
False

public virtual int Count{get;}

摘要:获取包含在System.Collections.Hashtable中的键/值对的数目。

          获取包含在System.Collections.SortedList中包含元素个数。

返回:包含在System.Collections.Hashtable中的键值对的数目

           包含在System.Collections.sortedList中的元素个数

获取Hashtable中包含的键值对个数/获取SortedList中元素个数

Hashtable hashtable = new Hashtable();                 //哈希表
hashtable.Add(1, "a");
hashtable.Add(2, "b");
Console.WriteLine(hashtable.Count);

--->
2
-------------------------------------------------
SortedList sortedList = new SortedList();              //排序列表

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");
sortedList.Add(4, "d");
 
Console.WriteLine(sortedList.Count);
 --->
4

public virtual int Capacity{get;set;}

摘要:获取包含在System.Coliections.SortedList对象的容量

返回:System.Coliections.SortedList对象可以包含的元素数目

Capacity获取或设置SortedList的容量

SortedList sortedList = new SortedList(5);     

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");
sortedList.Add(4, "d");
 
Console.WriteLine(sortedList.Count);             //获取排序列表的容量
Console.WriteLine(sortedList.Capacity);          //设置排序列表的容量
---> 
4
5

三、Hashtable类和SortedList类,哈希表与排序列表的一些常用方法

public virtual void Add(object key,object value)

摘要:将带有指定键或值得元素代入到System.Coliections.hashtable/SortedList对象。

  • Key:要添加的元素的键
  • value:要添加元素的值,该值可以为null;
Hashtable hashtable = new Hashtable();                 //哈希表
            hashtable.Add(1,'2');
            hashtable.Add(2, "qwe");
            foreach (DictionaryEntry item in hashtable)
            {
                Console.WriteLine(item.Key);
                Console.WriteLine(item.Value);
            }
---> 
1
2
2
qwe
-----------------------------------------------------------
SortedList sortedList = new SortedList();            //排序列表
            sortedList.Add(2, "qwe");
            sortedList.Add(1, "asd");
            sortedList.Add(3, "zxc");
            foreach (DictionaryEntry item in sortedList)
            {
                Console.WriteLine(item.Key);
                Console.WriteLine(item.Value);
            }
---> 
1
zxc
2
qwe
3
asd

public virtual void Clear();

摘要:从System.Coliections.hashtable/SortedList中移除所有元素。

从Hashtable/SortedList中移除元素

SortedList sortedList = new SortedList();            //排序列表
            sortedList.Add(2, "qwe");
            sortedList.Add(1, "asd");
            sortedList.Clear();                       //clear  清除了之前SortedList中的键值对
            sortedList.Add(3, "zxc");
            foreach (DictionaryEntry item in sortedList)
            {
                Console.WriteLine(item.Key);
                Console.WriteLine(item.Value);
            }
--------→
3
zxc
--------------------------------------------------------------------------------
Hashtable hashtable = new Hashtable();              //哈希表
            hashtable.Add(2, "qwe");
            hashtable.Add(1, "asd");
            hashtable.Clear();
            hashtable.Add(3, "zxc");
            foreach (DictionaryEntry item in hashtable)
            {
                Console.WriteLine(item.Key);
                Console.WriteLine(item.Value);
            }

public virtual void Contains(object key)

摘要: 确认是否包含制定键

  • Key:要在对象中查找的键

如果对象中包含指定元素KEy  ,如果有则输出True,没有输出False

Hashtable hashtable = new Hashtable();
            hashtable.Add(2, "qwe");
            hashtable.Add(1, "asd");
            hashtable.Add(3, "zxc");
Console.WriteLine(hashtable.Contoins(2));


---------------
True

public virtual void ContainsKey(object Key)

 此方法同上!

public virtual void ContainsValue(object value)

 摘要:确定是否包含指定值

  • Value:要在对象中查找的值

返回查找结果,是则为True 否则为false

代码方法同上

public virtual void Copyto(Array array,int arrayIndex)

 摘要:从System.Coliections.hashtable/SortedList中复制元素到到一维System.Arrar中

array:一维 System.Array 的目标 System.Collections.DictionaryEntry 从复制对象 System.Collections.Hashtable。
System.Array 必须具有从零开始的索引。
arrayIndex:array 中从零开始的索引,从此处开始复制。

复制元素到一维数组中

SortedList sortedList = new SortedList();

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c"); 

DictionaryEntry[] arr = new DictionaryEntry[5]; 
sortedList.CopyTo(arr,0);
foreach (var item in arr)
{
    Console.WriteLine(item.Key);
}
 
--->
1
2
3

更详细的源资料https://blog.csdn.net/NCZ9_/article/details/83990661  在我老师这  https://blog.csdn.net/NCZ9_/article/details/83990699

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值