C # Programming (32)----------Array Basics

Source: Internet
Author: User

Array

If you need to use multiple objects of the same type , you can use arrays . An array is a data structure that can contain multiple elements of the same type .

Declaration of an array

When declaring an array , you should first define the type of the elements in the array , followed by a pair of parentheses over the variable name . For example , life is an array that contains an integral type element :

int [] array;

Initialization of the array

When an array is declared, memory must be allocated to an array to hold all the elements of the arrays . An array is a reference type , so it must be allocated memory on the heap . To do this , you should initialize the array's variables by using the new operator, making the type and number of elements in the array . The size of the array is set below .

Array=new Int[4];

After declaring and initializing the array, the variable array refers to 4 integer values , which are located on the managed heap :

After you specify the size of the array, you cannot resize the array without copying all the elements in the array, and you can use the collection If you do not know beforehand how many elements in the array should be included.

Simplified operation for Declaration and initialization : int [] array=new int[4];

You can also use each element of an array initializer dimension array to assign a value . Array initializers can only be used when declaring array variables and cannot be used after declaring an array .

int [] array =new int [4] {1,2,3,4};

If you use curly braces to eat an array of paintings, you can also not specify the size of the array , because the compiler will automatically recognize the number of statistical elements :

int [] array =new int []{1,2,3,4};

A simpler approach :

int [] Array ={1,2,3,4};

Accessing array elements

After declaring and initializing the array , You can use the indexer to access its elements array only supports indexers with integer parameters

By passing the element number , you can access the array . Indexers always start with 0 . Represents the first element . The maximum value that can be passed to the indexer is the number of elements minus one , since the indexer starts at 0 .

int [] Array=new int[] {1,2,3,4};

int v1=array[0];

int v2=array[1];

array[3]=44;

Console.WriteLine (Arrar[4])// There will be an error .

If you use the wrong indexer value ( where the corresponding element does not exist ), a IndexOutOfRangeException exception is thrown .

If you do not know the number of elements in the array , you can use the Length property in the for statement :

for (int i=0;i<array. Length;++i)

{

Console.WriteLine (Arrray[i])

}

You can use the foreach statement in addition to using a For statement : foreach (var item in array)

{

Console.WriteLine (item);

}

Using reference types

In addition to declaring a predefined array , You can also declare a custom array .

public class Person

{

public string FirstName {get; set;}

public string LastName {get; set;}

public override string ToString ()

{

return string. Format ("{0},{1}", FirstName, LastName);;

}

}

Life an array containing two person elements is similar to an int array of life :

Person[] p = new person[2];

Note : If the elements in the array are reference types , you must allocate memory for each array element . If an element with unallocated memory in the array is used , an exception of type NullReferenceException is thrown.

Using the 0 -based indexer , You can allocate memory for each element of the array :

P[0] = new Person {FirstName = "hahaha", LastName = "Heiheihei"};

P[1] = new Person {FirstName = "hehehe", LastName = "Gagaga"};

The figure shows the person objects in the array in the managed heap .myperson is a variable stored on the stack , This variable references person< Span style= "font-family: Arial" > elements corresponding to expedited group . This array has enough space to accommodate two references . Each item in the array references a person object ,person objects are also stored on the managed heap

As with the int type , You can also use an array initializer for a custom type :

person [] p=

{

New Person {FirstName = "hehehe", LastName = "Gagaga"};

New Person {FirstName = "hahaha", LastName = "Heiheihei"};

}

Multidimensional arrays

A general array ( also known as an array ) is indexed by an integer . Multidimensional arrays are indexed by two or more integers .

To declare a two-dimensional array in C # , you need to add a comma to the square brackets . The size of each dimension ( also called the order) should be set when the array is initialized . Next , you can use two integers as indexers to access the elements in the array :

int[,]array=new int [n];

Array[0, 0] = 1;

Array[0, 1] = 2;

array[1, 0] = 3;

Array[1, 1] = 4;

After you declare an array , you cannot modify its order .

If you know the value of an element beforehand , you can use the array indexer to initialize the two-dimensional array . When initializing an array , an outer curly brace is used , and each line is initialized with an inner curly brace enclosed in an outer curly brace .

int[,] array = {

{A-i},

{4,5,6},

{7,8,9}

};

When you use an array initializer , you must initialize each element of the array and not expose any elements .

Using two commas in curly braces , you can have a three-dimensional array of life :

int[,,] array ={

{{1,2},{3,4}},

{{5,6},{7,8}},

{{9,10},{11,12}}

};

Console.WriteLine (array[0,1,1]);

Console.readkey ();

Jagged array

As can be seen , the two-dimensional array corresponds to a rectangle , the jagged array is flexible , in the jagged array , each row can have a different size .

When you declare a jagged array , you place the left and right brackets one at a time . When you initialize a jagged array , only the first line of square brackets sets the number of rows that the array contains . The second square bracket that defines the number of elements in each row is set to null , because each row of such an array

Contains the number of different elements . After that, specify the number of elements in the row for each row :

int[][] array = new int[3][];

Array[0] = new Int[2] {n};

ARRAY[1] = new Int[3] {a-i};

ARRAY[2] = new Int[4] {1,2,3,4};

Iterate through all the elements in the jagged array :

for (int row = 0; row < array. Length; row++)

{

for (int element = 0; element < Array[row]. Length; element++)

{

Console.WriteLine ("Row: {0}, element: {1}, Value: {2}", Row,element,array[row][element]);

}

}

The result of the operation is :

row:0, element:0, value:1

row:0, Element:1, Value:2

Row:1, element:0, value:1

Row:1, Element:1, Value:2

Row:1, Element:2, Value:3

Row:2, element:0, value:1

Row:2, Element:1, Value:2

Row:2, Element:2, Value:3

Row:2, Element:3, Value:4

C # Programming (32)----------Array Basics

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.