当前位置:网站首页>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);
}
}
}
边栏推荐
- NPM's nexus private server e401 E500 error handling record
- HBCK fix problem
- Spark SQL common time functions
- CGO is realy Cool!
- Distributed principle - what is a distributed system
- 解决vender-base.66c6fc1c0b393478adf7.js:6 TypeError: Cannot read property ‘validate‘ of undefined问题
- Notes on inputview and inputaccessoryview of uitextfield
- Use the command to check the WiFi connection password under win10 system
- spark分区算子partitionBy、coalesce、repartition
- Browser workflow (Simplified)
猜你喜欢

Application of C language array in Sanzi chess -- prototype of Queen n problem

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

Spark submission parameters -- use of files

MATLAB 如何生产随机复序列

MATLAB读取显示图像时数据格式转换原因

NPM's nexus private server e401 E500 error handling record

Idea eye care settings

带你创建你的第一个C#程序(建议收藏)

Node learning

分布式原理 - 什么是分布式系统
随机推荐
Tasks, micro tasks, queues and scheduling (animation shows each step of the call)
Single or multiple human posture estimation using openpose
理解“平均负载”
图论及概念
Instance Tunnel 使用
Maxcompute SQL 的查询结果条数受限1W
Object.prototype. Hasownproperty() and in
Spark sql 常用时间函数
Spark-SQL UDF函数
MATLAB读取显示图像时数据格式转换原因
4PAM在高斯信道与瑞利信道下的基带仿真系统实验
带你详细认识JS基础语法(建议收藏)
See a lot of blinking pictures on apps, especially the member page
Rediscluster setup and capacity expansion
Singleton mode 3-- singleton mode
Spark DF adds a column
Instance tunnel use
<栈模拟递归>
Promise object and macro task, micro task
Submarine cable detector tss350 (I)