当前位置:网站首页>C foundation in-depth learning II
C foundation in-depth learning II
2022-07-04 13:41:00 【Hua Weiyun】
C# Basic in-depth learning 02
Hashtable (Hashtable)
Hashtable Class represents a set of key based hash codes organized together key / value Yes . It USES key To access the elements in the collection .
When you use key When accessing elements , Use hash table , And you can identify a useful key value . Each item in the hash table has a key / value Yes . The key is used to access items in the collection .
Hashtable Methods and properties of class
The following table lists them Hashtable Class attribute :
| attribute | describe |
|---|---|
| Count | obtain Hashtable The number of key value pairs contained in . |
| IsFixedSize | Get a value , Express Hashtable Whether it has a fixed size . |
| IsReadOnly | Get a value , Express Hashtable Is it read-only . |
| Item | Gets or sets the value associated with the specified key . |
| Keys | Get one ICollection, contain Hashtable The key . |
| Values | Get one ICollection, contain Hashtable The value in . |
The following table lists them Hashtable Class Method :
| Serial number | Method name & describe |
|---|---|
| 1 | public virtual void Add( object key, object value ); towards Hashtable Add an element with the specified key and value . |
| 2 | public virtual void Clear(); from Hashtable Remove all elements from . |
| 3 | public virtual bool ContainsKey( object key ); Judge Hashtable Whether to include the specified key . |
| 4 | public virtual bool ContainsValue( object value ); Judge Hashtable Whether to include the specified value . |
| 5 | public virtual void Remove( object key ); from Hashtable Remove the element with the specified key from the . |
using System;using System.Collections;namespace day21test02{ class Program { /// <summary> /// Hashtable aggregate Hashtable /// Hash table order and insertion order , The order of values is irrelevant , Only with hash table key (hash operation ) The order of arrangement is related /// /// </summary> /// <param name="args"></param> static void Main(string[] args) { Hashtable ht = new Hashtable(10); // Duplicate values are not allowed , It's bulky ht.Add("id", 145); ht.Add("name", "q"); ht.Add("sex", 's'); // Determine whether the value exists in the hash table if (ht.Contains("sex")) { Console.WriteLine(ht["sex"]); } // You can overwrite the previously filled data , No exceptions ht["id"] = 45; Console.WriteLine("Hello World!"); } } class T { }}using System;using System.Collections;using System.Collections.Generic;namespace day21test03{ class Program { /// <summary> /// Hashtable and SortedList The sorting problem of /// hashtable There is no sorting method , Need help list /// </summary> /// <param name="args"></param> static void Main(string[] args) { // Primary method Hashtable ht = new Hashtable(20); ArrayList ay = new ArrayList(); ht.Add("k1", "v1"); ay.Add("k1"); ht.Add("k2", "v2"); ay.Add("k2"); ht.Add("k3", "v3"); ay.Add("k3"); ht.Add("k4", "v4"); ay.Add("k4"); ay.Sort(); for(int i = 0; i < ay.Count; i++) { Console.WriteLine(ht[ay[i].ToString()].ToString()); } // Advanced methods ,sortedList Automatic sorting , It is sorted when it is created SortedList sort = new SortedList(); sort.Add("kk1", "vv1"); sort.Add("kk2", "vv2"); sort.Add("kk3", "vv3"); sort.Add("kk4", "vv4"); Console.WriteLine("Hello World!"); } }}ArrayList and List Comparison
ArrayList
ArrayList Is a special array , By adding and deleting elements, you can dynamically change the length of the array .
ArrayList Advantages of sets over arrays : Support automatic resizing , You can insert elements flexibly , Can spirit
Delete elements alive , Flexible access to elements , however ArrayList Only one dimension is supported , And query and retrieve
Relatively slow .
stay C# There are two types of data in : Value type and reference type .
int bool char double enum struct DateTime And so on are value types ( Duplicate of value type and reference type
It should be characterized by , The length of the value type is fixed , The length of the reference type is not fixed ),string Array class
aggregate And so on are reference types . If it is a value type, it is stored directly in the stack ( local variable ), If it is a reference class
type , First store the data in the heap , Then store the address of the heap in the stack .
object A class is the base class for all classes , All data types can be converted to object class , That's why
ArrayList You can store value types and reference types , Because they are all converted into object Type storage
This is also ArrayList A disadvantage of , It is necessary to encapsulate the value type into object type , Take out
When you come, you need to object Type is then converted to value type , This process of packing and unpacking is very performance consuming
.
Packing : If you go ArrayList Store data of value type in , It needs to be converted into object Type save
Store , Encapsulated by value types object This type of process is called boxing .
Unpacking : from object The process of converting a type to a value type is called unpacking .
using System;using System.Collections;namespace day21test01{ class Program { /// <summary> /// ArrayList class /// The value type will be boxed as object type , When retrieving object Type unpacking is value type , The consumption of performance is relatively large . /// And it can only be one-dimensional /// </summary> /// <param name="args"></param> static void Main(string[] args) { ArrayList list = new ArrayList(); list.Add(1); list.Add("1"); list.Add('1'); list.Add(null); Console.WriteLine(list[0].GetType()); Console.WriteLine(list[1].GetType()); Console.WriteLine(list[2].GetType()); //Console.WriteLine(list[3].GetType()); Console.WriteLine("Hello World!"); } }}List
List By ArrayList A collection developed , Also have ArrayList be relative to Array The advantages of ,List Is a generic collection , It's solved ArrayList The disadvantage of unpacking and boxing for value types ,List When declaring, you need to specify the data type of the storage set
// Definition and initialization
// The first method
List <string> objList = new List<string>();
objList.Add(10);
objList.Add(2);
// The second method
int[] array = {1,2,3,4,5};
List<int> objList = new List<int>(array);// use array This array is used to fill
// The third kind of
List<string> nameList = new List<string>(){“ petty thief ”,“ The headmaster ”};
queue (Queue)
queue (Queue) Represents a fifo Object collection for . When you need fifo access to items , Use the queue . When you add an item to the list , be called The team , When you remove an item from the list , be called Out of the team .
Queue Methods and properties of class
The following table lists them Queue Class attribute :
| attribute | describe |
|---|---|
| Count | obtain Queue The number of elements contained in . |
The following table lists them Queue Class Method :
| Serial number | Method name & describe |
|---|---|
| 1 | public virtual void Clear(); from Queue Remove all elements from . |
| 2 | public virtual bool Contains( object obj ); To determine whether an element is present Queue in . |
| 3 | public virtual object Dequeue(); Remove and return in Queue The object at the beginning of . |
| 4 | public virtual void Enqueue( object obj ); towards Queue Add an object at the end of . |
| 5 | public virtual object[] ToArray(); Copy Queue Into a new array . |
| 6 | public virtual void TrimToSize(); Set the capacity to Queue The actual number of elements in . |
using System;using System.Collections;namespace day21test07{ class Program { /// <summary> /// queue , There are operations of joining and leaving the team /// </summary> /// <param name="args"></param> static void Main(string[] args) { Queue q = new Queue(); // The team q.Enqueue(12); q.Enqueue(24); q.Enqueue(36); // Out of the team , Automatic first out first in elements int a = (int)q.Dequeue(); // View the elements that currently need to be out of the team int b = (int)q.Peek(); Console.WriteLine("Hello World!"); } }}Point array (BitArray)
BitArray Class manages a compact array of bit values , It uses Boolean values to represent , among true Indicates that the bit is on (1),false Indicates that the bit is off (0).
When you need to store bits , But when you don't know the number of digits in advance , Then use point array . You can use Integer index Access items from the point array collection , Index starts from zero .
BitArray Methods and properties of class
The following table lists them BitArray Class attribute :
| attribute | describe |
|---|---|
| Count | obtain BitArray The number of elements contained in . |
| IsReadOnly | Get a value , Express BitArray Is it read-only . |
| Item | Get or set BitArray The value of the bit at the specified position in . |
| Length | Get or set BitArray The number of elements in . |
The following table lists them BitArray Class Method :
| Serial number | Method name & describe |
|---|---|
| 1 | public BitArray And( BitArray value ); For the current BitArray Elements in and specified BitArray The corresponding element in performs bitwise and operations . |
| 2 | public bool Get( int index ); obtain BitArray The value of the bit at the specified position in . |
| 3 | public BitArray Not(); Put the current BitArray The bit value in is reversed , To set to true And the element of that becomes false, Set to false And the element of that becomes true. |
| 4 | public BitArray Or( BitArray value ); For the current BitArray Elements in and specified BitArray Perform bitwise or operations on the corresponding elements in . |
| 5 | public void Set( int index, bool value ); hold BitArray Set the bit at the specified position in to the specified value . |
| 6 | public void SetAll( bool value ); hold BitArray Set all bits in the to the specified value . |
| 7 | public BitArray Xor( BitArray value ); For the current BitArray Elements in and specified BitArray To perform an operation on a corresponding or middle element . |
using System;using System.Collections;namespace day21test10{ class Program { /// <summary> /// BitArray /// </summary> /// <param name="args"></param> static void Main(string[] args) { // All data in the array is stored in binary form /*Byte[] a = new byte[1]; a[0] = 255; BitArray ary = new BitArray(a); foreach(var item in ary) { Console.WriteLine(item.ToString()); Console.WriteLine(item.ToString().ToLower() == "false"?0:1); } Console.WriteLine("Hello World!"); // An operator // Use with ( whole 1 take 1) or ( whole 0 take 0) Exclusive or ( Take the same 0, Different take 1) Binary operation is an efficient operation int n1 = 100; int n2 = 100; if ((n1 | n2) == 0) { Console.WriteLine(" equal "); }*/ //Console.WriteLine(); BitArray b1 = new BitArray(128); BitArray b2 = new BitArray(128); BitArray b3 = b1.Xor(b2); //if ((bool)b1.Xor(b2)) { } Console.Write(b3.ToString().ToLower() == "false"?0:1); } }}Stack (Stack)
Stack (Stack) Represents a Last in, first out Object collection for . When you need lifo access to items , Use the stack . When you add an item to the list , be called push Elements , When you remove an item from the list , be called eject Elements .
Stack Methods and properties of class
The following table lists them Stack Class attribute :
| attribute | describe |
|---|---|
| Count | obtain Stack The number of elements contained in . |
The following table lists them Stack Class Method :
| Serial number | Method name & describe |
|---|---|
| 1 | public virtual void Clear(); from Stack Remove all elements from . |
| 2 | public virtual bool Contains( object obj ); To determine whether an element is present Stack in . |
| 3 | public virtual object Peek(); Back in the Stack The top object of , But don't remove it . |
| 4 | public virtual object Pop(); Remove and return in Stack The top object of . |
| 5 | public virtual void Push( object obj ); towards Stack Add an object at the top of . |
| 6 | public virtual object[] ToArray(); Copy Stack Into a new array . |
using System;using System.Collections;namespace day21test08{ class Program { /// <summary> /// Stack , Last in first out collection class ( The last to enter the stack is the first to exit the stack ) /// There are mainly stack Out of the stack /// </summary> /// <param name="args"></param> static void Main(string[] args) { Stack ss = new Stack(); // Pressing stack ss.Push(12); ss.Push(13); ss.Push(14); // Out of the stack int c = (int)ss.Pop(); Console.WriteLine(c); // The current data to be published is int d = (int)ss.Peek(); Console.WriteLine(d); Console.WriteLine("Hello World!"); } }}边栏推荐
猜你喜欢

Practice: fabric user certificate revocation operation process

强化学习-学习笔记1 | 基础概念

HAProxy高可用解决方案

Zhongang Mining: in order to ensure sufficient supply of fluorite, it is imperative to open source and save flow

runc hang 导致 Kubernetes 节点 NotReady

Alibaba cloud award winning experience: build a highly available system with polardb-x

Comparative study of the gods in the twilight Era

《预训练周刊》第52期:屏蔽视觉预训练、目标导向对话

干货整理!ERP在制造业的发展趋势如何,看这一篇就够了

实时云交互如何助力教育行业发展
随机推荐
数据库锁表?别慌,本文教你如何解决
"Pre training weekly" issue 52: shielding visual pre training and goal-oriented dialogue
C#基础深入学习一
runc hang 导致 Kubernetes 节点 NotReady
Talk about the design and implementation logic of payment process
干货整理!ERP在制造业的发展趋势如何,看这一篇就够了
诸神黄昏时代的对比学习
C#/VB. Net to add text / image watermarks to PDF documents
WPF double slider control and forced capture of mouse event focus
一个数据人对领域模型理解与深入
请问大佬们有遇到这个情况吗,cdc 1.4 连接MySQL 5.7 无法使用 timestamp
PostgreSQL 9.1 soaring Road
使用Scrcpy投屏
How real-time cloud interaction helps the development of education industry
runc hang 导致 Kubernetes 节点 NotReady
[AI system frontier dynamics, issue 40] Hinton: my deep learning career and research mind method; Google refutes rumors and gives up tensorflow; The apotheosis framework is officially open source
游戏启动后提示安装HMS Core,点击取消,未再次提示安装HMS Core(初始化失败返回907135003)
Xue Jing, director of insight technology solutions: Federal learning helps secure the flow of data elements
阿里云有奖体验:用PolarDB-X搭建一个高可用系统
Web知识补充