当前位置:网站首页>C # fine sorting knowledge points 9 Set 2 (recommended Collection)
C # fine sorting knowledge points 9 Set 2 (recommended Collection)
2022-07-25 15:27:00 【꧁ small ۣۖ Pigeon ۣۖ Puzi ۣۖ ิ꧂】
4. Stack
Stack ( Stack ) Is one of the common data structures , Stack is a first in and last out structure , That is, the element is inserted from the end of the stack , Remove from the end of the stack , It is similar to loading cars when moving in daily life , First load the things on the car and then take them down .
In the collection Stack Class simulates stack operations , Provides properties and methods commonly used in the stack .
Stack Class provides 3 Construction methods , As shown in the following table :
| Construction method | effect |
|---|---|
| Stack() | Use initial capacity to create Stack The object of |
| Stack(ICollection col) | establish Stack Example , This instance contains elements copied from the specified instance , And the initial capacity and the number of copied elements 、 Same growth factor |
| Stack(int capacity) | establish Stack Example , And set its initial capacity |
Stack The common properties and methods in the class are shown in the following table :
| Properties or methods | effect |
|---|---|
| Push(object obj) | Add elements to the stack , Also called stack |
| object Peek() | Used to get the value of the top element of the stack , But do not remove the value of the top element of the stack |
| object Pop() | The value used to remove the top element of the stack , And remove the top element |
| Clear() | from Stack Remove all elements from |
| Contains(object obj) | To determine whether an element is present Stack in |
| object[] ToArray() | Copy Stack Into a new array |
example :
class Program
{
static void Main(string[] args)
{
// Simulate the access of restaurant dishes .
Stack stack = new Stack();
// Put elements into the stack
stack.Push("1 Plate No ");
stack.Push("2 Plate No ");
stack.Push("3 Plate No ");
stack.Push("4 Plate No ");
stack.Push("5 Plate No ");
Console.WriteLine(" Take out the plate :");
// Determine if there are elements in the stack
while(stack.Count != 0)
{
// Take out the elements in the stack
Console.WriteLine(stack.Pop());
}
}
}
5. Hashtable ( Hash table )
Hashtable Class implements the IDictionary Interface , The values in the collection are accessed in the form of key value pairs . Element order according to key Of hash Values .
C# Medium Hashtable Called hash table , Also known as hash table , Use key value pairs in this set (key/value) Store values in the form of .
Besides , It also provides information about the elements in the collection key Value to find its corresponding value The method of value .
Hashtable Class provides many construction methods , The most common construction method without parameters , That is, instantiate through the following code Hashtable class .
Hashtable Object name = new Hashtable();
Hashtable The properties and methods commonly used in the class are shown in the following table :
| Properties or methods | effect |
|---|---|
| Count | The actual number of elements stored in the collection |
| void Add(object key,object value) | Add elements to the collection |
| void Remove(object key) | According to the designation key Value to remove the corresponding collection element |
| void Clear() | Empty the set |
| ContainsKey (object key) | Determines whether the set contains the specified key The value of the elements |
| ContainsValue(object value) | Determines whether the set contains the specified value The value of the elements |
example :
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add(1," Fundamentals of computer ");
ht.Add(2,"C# Advanced programming ");
ht.Add(3," Database application ");
Console.WriteLine(" Please enter the book number ");
int id = int.Parse(Console.ReadLine());
bool flag = ht.ContainsKey(id);
if(flag)
{
Console.WriteLine(" The name of the book you are looking for is :{0}",ht[id].ToString());
}
else{
Console.WriteLine(" The book number you are looking for does not exist !");
}
Console.WriteLine(" All book information is as follows :");
foreach(DictionaryEntry d in ht)
{
int key = (int)d,key;
string value = d.Value.ToString();
Console.WriteLine(" Book number :{0}, The name of the book :{1}",key.value);
}
}
}
6. Ordered list
SortedList Class implements the IDictionary Interface , The values in the collection are accessed in the form of key value pairs .C# SortedList It is called a sequence table , according to key Value sorts the elements in the collection .
SortedList The properties and methods commonly used in the class are shown in the following table :
| Properties or methods | effect |
|---|---|
| Count | The actual number of elements stored in the collection |
| void Add(object key,object value) | Add elements to the collection |
| void Remove(object key) | According to the designation key Value to remove the corresponding collection element |
| void Clear() | Empty the set |
| ContainsKey (object key) | Determines whether the set contains the specified key The value of the elements |
| ContainsValue(object value) | Determines whether the set contains the specified value The value of the elements |
example :
class Program
{
static void Main(string[] args)
{
SortedList st = new SortedList();
st.Add(1," Xiao Zeng ");
st.Add(2," Xiaojia ");
st.Add(3," Xiaoyi ");
Console.WriteLine(" Please enter the registration number ");
int id = int.Parse(Console.ReadLine());
bool flag = st.ContainsKey(id);
if(flag)
{
string name = st[id].ToString();
Console.WriteLine(" The name of the patient you are looking for is :{0}",);
}
else{
Console.WriteLine(" The registration number you are looking for does not exist !");
}
Console.WriteLine(" All patient information is as follows :");
foreach(DictionaryEntry d in st)
{
int key = (int)d,key;
string value = d.Value.ToString();
Console.WriteLine(" Registration number :{0}, full name :{1}",key.value);
}
}
}
边栏推荐
- matlab---错误使用 var 数据类型无效。第一个输入参数必须为单精度值或双精度值
- Spark-SQL UDF函数
- Once spark reported an error: failed to allocate a page (67108864 bytes), try again
- Delayed loading source code analysis:
- redis淘汰策列
- Xcode添加mobileprovision证书文件报错:Xcode encountered an error
- Understanding the execution order of T-SQL query from the execution order of join on and where
- UITextField的inputView和inputAccessoryView注意点
- 你准备好脱离“内卷化怪圈”了吗?
- Reflection - Notes
猜你喜欢

ML - 自然语言处理 - 自然语言处理简介

4PAM在高斯信道与瑞利信道下的基带仿真系统实验

Spark submission parameters -- use of files

Spark SQL null value, Nan judgment and processing

Solve the timeout of dbeaver SQL client connection Phoenix query

Use the command to check the WiFi connection password under win10 system

Reflection - Notes

分布式原理 - 什么是分布式系统

Idea eye care settings

ML - 语音 - 深度神经网络模型
随机推荐
ML - 语音 - 语音处理介绍
How much memory can a program use at most?
ML - natural language processing - Introduction to natural language processing
Idea护眼色设置
Redis elimination strategy list
Instance Tunnel 使用
记一次redis超时
Outline and box shadow to achieve the highlight effect of contour fillet
本地缓存--Ehcache
Find out what happened in the process of new
Spark judges that DF is empty
Idea远程提交spark任务到yarn集群
Example of password strength verification
Maxcompute SQL 的查询结果条数受限1W
What is the Internet of things
window系统黑窗口redis报错20Creating Server TCP listening socket *:6379: listen: Unknown error19-07-28
盒子躲避鼠标
Word 样式模板复制到另一文档
谷歌云盘如何关联Google Colab
Rediscluster setup and capacity expansion