A brief introduction to C # staggered array _c# tutorial

Source: Internet
Author: User
Tags array example arrays first row
C # Arrays have a lot to learn, here we mainly introduce C # staggered arrays, including the introduction of one-dimensional array examples, two-dimensional array examples, C # staggered arrays and so on.

Arrays are often used in our programming, think we have a certain concept of the group, this part of the content will give you a detailed explanation of the array in C # How to define and use.

Definition: An array is a set of values of the same type, the array is a reference type, and therefore exists in the memory heap. The value of an element in an array can be assigned when the array is defined, or it can be assigned to a single element after the array is defined.

one-dimensional array example:
Copy Code code as follows:

public static void Main ()
{
One-dimensional arrays
int[] Inti = new Int[3] {1, 2, 3};
Defines an array inti and defines that it can have only 3 elements, the value being 1,2,3
INTI[1] = 4;//change inti[1] that is, the value of the second element is 4.
Console.WriteLine (Inti[0], inti[1], inti[2]);
}

Two-dimensional array example:
Copy Code code as follows:

public static void Main ()
{
Two-dimensional array
int[,] inti = new int[2, 3] {1, 2, 3}, {4, 5, 6}};
Defines a two-dimensional array inti of 2 rows of 3 cases and assigns values to them.
inti[1, 0] = 5;//change inti[1,0] that is the second example, the first row of the element's value is 5.
for (int i = 0; i < Inti. GetLength (0); i++)
Inti. The GetLength () method gets the number of elements in the specified dimension for the array.
{
for (int j = 0; J < Inti. GetLength (1); J + +)
The first for loop iterates through the columns of a two-dimensional array, and the second for loop iterates through the rows of the two-dimensional array.
{
Console.WriteLine (Inti[i, J]);
Prints the value of the specified element in the Inti array.
}
}
}

Note: We can change the value in [2,3], just to change the number of rows and columns in a two-dimensional array, and to increase the number of dimensions, we can say [2,3,2].
C # Jagged arrays:

Next, we introduce a very personalized array called an array of arrays (C # jagged Array)

Copy Code code as follows:

public static void Main ()
{
Jagged array
int[][] Inti = new int[3][];
Unlike a one-dimensional or multidimensional array, a jagged array needs to define two [] numbers,
You must specify the number of the first subscript, without specifying the initial point.
Inti[0] = new int[] {1, 2, 3};//assigned to a jagged array
INTI[1] = new int[] {3, 4, 5, 6, 7};
INTI[2] = new int[] {3, 4};
for (int i = 0; i < Inti. Length; i++)
Number of elements with Inti as number of loops
{
for (int j = 0; J < Inti[i]. Length; J + +)
Number of elements in Inti[i] is the number of cycles
{
Console.Write (Inti[i][j]);
Print a jagged array
}
Console.WriteLine ();
Wraps the Inti array element.
}
}


Note: We see that the two-dimensional array is similar to the result of the C # interleaved array printing, note, however, that each array within a C # staggered array can be a dimension, and unlike a two-dimensional array, columns have rows, and the element values for each array in a C # staggered array can be different.
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.