Learn the various data structures

Source: Internet
Author: User

array : An array in which he can store a fixed amount of data of the same type, which can be indexed to the corresponding value.

If the value type is stored: for example int[] arr=new int[2]; So the variable arr on the stack, he references an int value on the two managed heap.

If a reference type is stored: for example person[] arr=new person[2]; then the variable arr, on the stack, refers to an array of references that store the person class on the managed heap.

Array class : is an abstract class, he implements the ICloneable, IList, ICollection, IEnumerable these several interfaces

Copy:

and ICloneable implements Clone () he is creating a shallow surface copy: If it is a value type, the thing he replicates is all the values. If it is an app type, it copies the reference, the new array operation, or the same memory.

Copy is also a shallow copy, the difference is: Clone directly creates a new array, copy to have the same dimension, and enough capacity to store copies.

If you need a deep copy of the reference type array, you want to iterate over the algebraic group to create the new object.

Sort: The array class uses the fast algorithm to sort the elements in the array, and the sort () method requires that the elements in the arrays implement the IComparable interface. Array.Sort (strings), the simple type string int32 implements this excuse.

IComparable interface A CompareTo () to use sort () to implement it, equal to return 0, if the instance is ranked before the parameter method returns a number less than 0, the instance is returned after the argument with a parameter greater than 0. If you want to implement a different sort method that implements the Icompare interface, and or Icompare<t> interface, the Icompare class is independent of the class to be compared so that it can be done by Array.Sort (Persons,new personcompare) The comparison.

Ieunmerator Interface: Enumeration interface, GetEnumerator () method, returns an enumeration that implements the Ieunmerator interface, which he defines

BOOL MoveNext ();
Object current {get;}
void Reset ();

Three methods.

foreach uses an enumeration

Using yield iterations makes it easy to create enumerators,

 Public   class  Gge
{
Realize
GetEnumerator ()
 Public Ienumerator<string> GetEnumerator ()   {        yieldreturn9;         yield return 6 ;           }}    

You can use the foreach iteration gge.

A method or property that contains a yield statement, called an iteration block, must return a IEnumerator, or IEnumerable interface, and a compile-time yield statement generates an enumerator.

By usingyield defines iterators that do not require additional explicit classes (classes that preserve enumeration state) when implementing the IEnumerable and IEnumerator modes of a custom collection type, for example, see IEnumerator<t > )。

public class powersof2{    static void Main ()    {        //Display powers of 2 up to the exponent of 8:        foreach (int I in Power (2, 8))        {            Console.Write ("{0}", i);        }    }    public static system.collections.generic.ienumerable<int> Power (int number, int exponent)    {        int result = 1;        for (int i = 0; i < exponent; i++)        {            result = result * number;            yield return result;        }    }    Output:2 4 8 16 32 64 128 256}

Using the iteration block compiler generates a yield type, which contains a state machine, to be perfected.

Collection: The array size is fixed, and the collection is used if you want to store the number of dynamic elements. The collection classes have list<t>, queues, stacks, lists, dictionaries, and sets.  The set of sub-generic classes has ArrayList HashTable. The thread-Safe collection class is located in System.Conllection.Concurrent.

Dynamic list with list<t > implemented IList, ICollection, IEnumerable, ilist<t>, icollection<t>, IEnumerable  <t>, while ArrayList is a non-generic list. list<t> constructs an empty list by default, adding elements will build 4 elements, which, if exceeded, will expand twice times. If the capacity changes, the content is reassigned to the new memory. Creates a new array, via Array.copy (). So in advance of knowing his capacity, given capacity, the efficiency will be higher.

There are initial value settings, adding elements, inserting elements, accessing elements, deleting elements, receiving, sorting, type conversion, and read-only collections.

Queue : Queue FIFO,queue<t> implements ICollection and IEnumerable <T> interface without implementing Iconllection<t> interface, add Remove method cannot be used for queues. The Ilist<t> interface is not implemented, and the queue cannot be accessed with an indexer. Implemented internally with t[]. Cond

stack: advanced post-out structure, in the Foreach method, the IEnumerable interface iterates over all elements, and the stack enumerator does not delete the element.

Linked list: With Cont.

Dictionary: A dictionary is a complex data structure that can find values quickly, depending on the key. You can also freely add and remove elements. A bit like list<t>, but there is no memory to move the performance overhead of subsequent elements.

The type of the key in the dictionary must override the GetHashCode () method of the object class, because the dictionary determines the position of the element, and he wants to call the int returned by GetHashCode () to calculate the index of the element placed at the corresponding position. It involves prime numbers, and the dictionary's capacity is a prime number. The new ability of the dictionary depends on the Code of GetHashCode ().

The key type also needs to be rewritten iequatable<t>. Equals () method. or override the Equals () method of object. If A.equals (B) returns True, then A.gethashcode () and B.gethashcode (); If the design of some way to override these methods does not meet this condition, and use it as a key, there will be an index is not the value of the phenomenon.

The Equals method compares a reference, and GetHashCode () returns a hash code based only on the object's address. If the hash list is based on a key and the build does not override these methods, the hash list will work correctly. However, the keys are considered equal only if the objects are exactly the same.

String implements the IEquatable interface and overloads the GetHashCode () method, and Equals () provides a comparison of the values, and GetHashCode () returns a hash-list code based on the value of the string. It is convenient to use a string as the key value in a dictionary.

Int32 is not suitable for use in dictionaries. These types of return hash codes are mapped only to values. If the value you want to use as the key itself is not distributed within the range of possible integers, it is not good to use an integer as the key value.

Learn the various data structures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.