C # Array Detailed

Source: Internet
Author: User
An array is a data structure that is declared in the following way:

Type[] Arrayname;

The array has the following properties:

1. The array can be one-dimensional, multidimensional, or interleaved.
2. The default value of a numeric array element is set to zero, and the default value of the reference element is set to NULL.
3. A jagged array is an array of arrays, so its elements are reference types and are initialized to null.
4. The index of the array is zero-based: The index of an array with n elements is from 0 to n-1.
5. Array elements can be of any type, including array types.

One-dimensional arrays

Declares a one-dimensional array, without initialization, equal to null   int[] intArray1;   Initializes a declared one-dimensional array   intArray1 = new int[3];//The default value of the array element is 0   intArray1 = new int[3]{1,2,3};   IntArray1 = new int[]{1,2,3};       Declares a one-dimensional array while initializing   int[] intArray2 = new int[3]{1,2,3};   int[] intArray3 = new int[]{4,3,2,1};   Int[] IntArray4 = {1,2,3,4};   string[] StrArray1 = new string[]{"One", "one", "one", "three"};   String[] StrArray2 = {"This", "was", "an", "string", "Array"};

Multidimensional arrays

Declares a two-dimensional array, without initializing  short[,] sArray1;  Initializes a declared two-dimensional array  sArray1 = new short[2,2];  SArray1 = new short[2,2]{{1,1},{2,2}};  SArray1 = new short[,]{{1,2,3},{4,5,6}};    Declares a two-dimensional array while initializing  short[,] sArray2 = new short [1,1]{{100}};  short[,] sArray3 = new short [,]{{1,2},{3,4},{5,6}};  short[,] SArray4 = {{1,1,1},{2,2,2}};  Declares a three-dimensional array while initializing  byte[,] BArray1 = {{{1,2},{3,4}},{{5,6},{7,8}}};

Jagged arrays

Declares a jagged array without initializing  int[][] JagIntArray1;  Initializes a declared jagged array  JagIntArray1 = new int [2][] {      new int[]{1,2},      new int[]{3,4,5,6}     };  JagIntArray1 = new int [][]{       new int[]{1,2},      //new int []{3,4,5},       intArray2//using int[] array variable      };  Declare a jagged array while initializing  int[][] JagIntArray2 = {      new int[]{1,1,1},      //new int []{2,2},      intArray1      };

An array is a group of identical types that are grouped together, using a generic name, to access the elements in the data collection by assigning the subscript.

An array is a set of data that has the same type. When you access data in an array, you can specify it by subscript. Array elements in C # can be any data type, array subscript starts with 0, that is, the first element corresponds to the subscript 0, and then increments one after the other. Arrays can be one-dimensional or multidimensional.

One-dimensional arrays are the most basic types of arrays, and they are declared as follows:

data type [] array name;

Example:
int [] Anarray; Declares a one-dimensional array of integral types

An array with two dimensions is a two-dimensional array that is declared as follows:
data type [,] array name;
Example:

int [,] anarray; Declares a two-dimensional array of integral types
float [,]anarrayoffloats; Declaring a two-dimensional array of floating-point types
string [,] anarrayofstrings; Declares a two-dimensional array of string literals

When declaring an array variable, an array is not created yet, and no memory space is allocated for the elements in the array, so after declaring the arrays, you need to instantiate them:

Anarray = new int [2,4];
anarrayofstrings = new stirng [2,4];

We can also initialize the array element with the given value.

int [,] anarray = new int [2, 4] {{1,2,3,4},{5,6,7,8}};
string [,] anarrayofstrings = new string [2, 2] {{"tithing", "Person B"}, {"Champion", "runner-Up"}};

You can also use the following shortcuts:

int [,] Anarray = {{0,1,2,3},{1,2,3,4}};
string [,] anarrayofstrings = {{"tithing", "Person B"}, {"Champion", "runner-Up"}};

In the C # language, arrays provide us with some useful features that allow us to accomplish some of the more advanced features.
The array name. Length: Returns an integer that represents the total number of elements in all the dimensions of the array.
The array name. Rank: Returns an integer that represents the number of dimensions of the array.
The array name. GetLength (int dimension): Returns an integer that represents the number of elements in the specified dimension of the array (specified by the parameter dimension, zero-based on the dimension).

The 4.foreach statement loops through the embedded statement for each element in the array or collection.
The syntax format for a foreach statement is:
foreach (data type identifier in expression)
Embed statement

An array of one-dimensional integers containing 6 elements;
Int[] Mf1=new int[6]; Note the scope of the initialized array, or specify the initial value;

An array of one-dimensional integers containing 6 elements, initial 1,2,3,4,5,6
Int[] Mf2=new int[6]{1,2,3,4,5,6};

A one-dimensional array of strings that can also omit the new operator if an initializer is provided
String[] mf3={"C", "C + +", "C #"};

One-dimensional object array
object[] Mf4 = new Object[5] {26, 27, 28, 29, 30};

Two-dimensional integer array, initial value mf5[0,0]=1,mf5[0,1]=2,mf5[1,0]=3,mf5[1,1]=4
int[,] mf5=new int[,]{{1,2},{3,4}};

6*6 array of two-dimensional integers
int[,] mf6=new mf[6,6];

Let's look at the traversal of a one-dimensional string array

Using System; public class Mikecat {static void PrintArray (string[] arr) {//print array element, arr. Length indicates the number of array elements for (int i=0;i<arr. length;i++) {Console.WriteLine ("Arr[{0}]={1}", I,arr[i]);}} public static void Main () {string[] arr={"C", "C + +", "C #"};//Passing an array as a parameter PrintArray (arr);}}

Program Result: Arr[0]=c arr[1]=c++ arr[2]=c#

Let's look at a traversal of an integer array of 4 rows and 2 columns (4*2):

Using System; public class Mikecat {static void PrintArray (int[,] arr) {//through two-time for loop traversal of the two-dimensional array for (int i=0;i<4;i++)//Initialize I as a loop variable, i++ implement the change The self-increment operation of the volume. The For loop executes the i++ after the condition has been fulfilled and then enters the next loop. Simple c syntax, here to do a brief introduction for beginners to learn. (See the book "Advanced Programming in C # 4.0" for details) {for (int j=0;j<2;j++) {Console.WriteLine ("Arr[{0},{1}]={2}", I,j,arr[i,j]);//print each two-dimensional array element}}} public static void Main () {//main function//pass array as a parameter PrintArray (new int[,]{{1,2},{3,4},{5,6},{7,8}};}}

The above is the C # array of detailed content, more related articles please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.