C # select and sort out key points of knowledge 9 Set 2 (recommended Collection)

Posted by stephenf33 on Sun, 19 Dec 2021 16:11:17 +0100

4. Stack

Stack is one of the common data structures. Stack is a first in and last out structure, that is, elements are inserted from the tail of the stack and removed from the tail of the stack. It is similar to loading when moving in daily life. The things loaded first should be taken down later.

The Stack class in the collection simulates Stack operations and provides common properties and methods in the Stack.

Stack class provides three construction methods, as shown in the following table:

Construction methodeffect
Stack()Create Stack objects using initial capacity
Stack(ICollection col)Create an instance of Stack, which contains elements copied from the specified instance, and the initial capacity is the same as the number of copied elements and growth factor
Stack(int capacity)Create an instance of Stack and set its initial capacity

The common attributes and methods in Stack class are shown in the following table:

Property or methodeffect
Push(object obj)Adding elements to the stack is also known as stacking
object Peek()Used to get the value of the top element of the stack, but does not remove the value of the top element
object Pop()Used to remove the value of the stack top element and remove the stack top element
Clear()Remove all elements from the Stack
Contains(object obj)Determine whether an element is in the Stack
object[] ToArray()Copy Stack to a new array

Example:

class Program
{
    static void Main(string[] args)
    {
         //Simulate the access of restaurant plates.
        Stack stack = new Stack();
        //Store elements in 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("Remove the plate:");
        //Determine whether there are elements in the stack
        while(stack.Count != 0)
        {
            //Take out the elements in the stack
            Console.WriteLine(stack.Pop());
        }
    }
}

5. Hash table (hash table)

The Hashtable class implements the IDictionary interface, and the values in the collection are accessed in the form of key value pairs. The element order is arranged according to the hash value of the key.

The Hashtable in C # is called a hash table, also known as a hash table. In this set, values are stored in the form of key/value pairs.

In addition, it also provides a method to find its corresponding value value according to the key value of the element in the collection.

There are many construction methods provided by the Hashtable class. The most common is the construction method without parameters, that is, instantiate the Hashtable class through the following code.

Hashtable Object name = new Hashtable();

The properties and methods commonly used in Hashtable class are shown in the following table:

Property or methodeffect
CountThe actual number of elements stored in the collection
void Add(object key,object value)Add elements to the collection
void Remove(object key)Removes the corresponding collection element according to the specified key value
void Clear()Empty collection
ContainsKey (object key)Determines whether the collection contains elements with the specified key value
ContainsValue(object value)Determines whether the collection contains the element with the specified value

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 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 No.:{0},Book Name:{1}",key.value);
        }
    }
}

6. Ordered list

The SortedList class implements the IDictionary interface, and the values in the collection are accessed in the form of key value pairs. C# SortedList is called a sequenced list, which sorts the elements in the set according to the key value.

The common attributes and methods in SortedList class are shown in the following table:

Property or methodeffect
CountThe actual number of elements stored in the collection
void Add(object key,object value)Add elements to the collection
void Remove(object key)Removes the corresponding collection element according to the specified key value
void Clear()Empty collection
ContainsKey (object key)Determines whether the collection contains elements with the specified key value
ContainsValue(object value)Determines whether the collection contains the element with the specified value

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 No.:{0},full name:{1}",key.value);
        }
    }
}

Topics: C#