当前位置:网站首页>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);
}
}
}
边栏推荐
- What is the Internet of things
- Graph theory and concept
- 苹果内购和Apple Pay 的区别
- UITextField的inputView和inputAccessoryView注意点
- Distributed principle - what is a distributed system
- Node learning
- Run redis on docker to start in the form of configuration file, and the connection client reports an error: server closed the connection
- How to update JSON values in the database?
- Submarine cable detector tss350 (I)
- Xcode添加mobileprovision证书文件报错:Xcode encountered an error
猜你喜欢

从 join on 和 where 执行顺序认识T-sql查询执行顺序

Idea eye care settings

盒子躲避鼠标

window系统黑窗口redis报错20Creating Server TCP listening socket *:6379: listen: Unknown error19-07-28

JVM知识脑图分享

Single or multiple human posture estimation using openpose

Distributed principle - what is a distributed system

ML - 自然语言处理 - 基础知识

Spark AQE

Reflection - Notes
随机推荐
JVM dynamic bytecode technology details
记一次redis超时
本地缓存--Ehcache
Spark AQE
Tasks, micro tasks, queues and scheduling (animation shows each step of the call)
Notes on inputview and inputaccessoryview of uitextfield
Spark 判断DF为空
NPM's nexus private server e401 E500 error handling record
期货在线开户是否安全?去哪家公司手续费最低?
CGO is realy Cool!
Week303 of leetcode
Use the command to check the WiFi connection password under win10 system
图论及概念
Spark SQL空值Null,NaN判断和处理
Vscode plugin collection
Es5 thinking of writing inheritance
CGO is realy Cool!
No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac
从 join on 和 where 执行顺序认识T-sql查询执行顺序
Graph theory and concept