Vii. Arrays and collections (Declaration and use of one-dimensional arrays and two-dimensional arrays, common sets (generics and non-generics), ArrayList class, Hashtable,list,directory dictionary, etc.)

Source: Internet
Author: User

Array

An array is a collection, a set of values of the same data type, which is a combination of fixed number of elements of the same type.

An array of arrays of reference types starts at 0, meaning that if you want to access an array element, you have to access it by the integer index number of the corresponding element.

Define a one-dimensional array: array type [] array name =new data type [number of elements];

int [] arrmyint= new int [10];//defines an array

The data type of the array is int, with 10 elements.

int [] arrmyint; Declaring a one-dimensional array

Arrmyint =new int [10]; Allocating storage space to an array

Initialize array-Dynamic initialization: You must use the New keyword when initializing an array, and you must declare the size of the array when initializing the arrayint[] Arrintarray;arrintarray=New int[3];//can hold 3 integersarrintarray[0]= A;//assign a value to the first element of an arrayarrintarray[1]= -;//assign a value to the second element of an arrayarrintarray[2]=6; You can also write definitions and dynamic initialization together:string[] arrmystring=New string[3]{“ as"," VB "," at"}; Initialize array-static initialization: When you initialize an array with this method, you do not need to describe the number of elements in the arrays, simply list all the elements in the array in order, and the system automatically calculates and allocates the required memory space for the array. int[] arrpoints={- $,9, in, +, $};//An integer array that defines a 5-elementChar[] arrletters={'a','b','C','D'};//defines a 4-element character array

The initial value of the array

When you add a value type to the array initialization value is

Numeric types are initialized to 0

Boolean type is initialized to False

Character type is initialized to null

Enum type is initialized to 0

Two-dimensional arrays

//definition and initialization of two-dimensional arraysDouble[,] arrdouble=New Double[3,4 ] ;//when you dynamically initialize a two-dimensional array, you can also give it an initialization value directly. int[,] arrint=New int[ , ] {{1,3}, {2,4}, {5,6}};//a two-dimensional array can also be statically initializedhar[,] chrletter={{' A ', ' B ', ' C '}, {' X ', ' y ', ' Z '}};

ArrayList class

A dynamic Array (ArrayList) represents an ordered collection of objects that can be individually indexed. It can basically replace an array.

However, unlike arrays, you can use an index to add and remove items at a specified location, and the dynamic array will automatically resize it.

It also allows dynamic memory allocations, additions, searches, and sorting of items in a list.

ArrayList Al =NewArrayList (); Console.WriteLine ("Adding some numbers:"); Al. ADD ( $); Al. ADD ( +); Al. ADD ( -); Al. ADD ( About); Al. ADD ( A); Al. ADD ( at); Al. ADD (9); Console.WriteLine ("capacity: {0}", AL.            capacity); Console.WriteLine ("Count: {0}", AL.                                  Count); Console.Write ("Content:"); foreach(intIinchal) {Console.Write (i+" ");            } Console.WriteLine (); Console.Write ("Sorted Content:"); Al.            Sort (); foreach(intIinchal) {Console.Write (i+" ");            } Console.WriteLine (); Console.readkey ();//when the above code is compiled and executed, it produces the following resultsAdding some numbers:capacity:8Count:7Content: $  +  -  About  A  at 9Content:9  A  at  -  $  About  +    

Hashtable class

The Hashtable class represents a series of key /value pairs that are organized by key-based hash codes. It uses keys to access the elements in the collection.

When a key is used to access an element, a hash table is used, and a useful key value can be identified. Each item in the hash table has a key/value pair. Key to access the items in the collection.

List

Owning namespace: System.Collections.Generic

The List<t> class is a generic equivalent class of the ArrayList class. This class implements the Ilist<t> generic interface using an array of size that can be dynamically incremented on demand.

The benefits of generics: It adds great potency and flexibility to writing object-oriented programs using the C # language. Performance is improved by not forcing boxing and unpacking of value types, or by forcing type conversions down on reference types.

Directory

In C #,Dictionary provides quick, part-based element lookups. It is structured like this:Dictionary<[key], [value]> , which can be used when there are many elements.

It is contained in the System.Collections.Generic name space. Before you use it, you must declare its key type and value type.

Any key must be unique, the key cannot be null, and if the value is a reference type, it can be a null value.

Creation and initialization of:dictionary<int,string> mydictionary=new dictionary<int,string> ();

Vii. Arrays and collections (Declaration and use of one-dimensional arrays and two-dimensional arrays, common sets (generics and non-generics), ArrayList class, Hashtable,list,directory dictionary, etc.)

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.