C # Analysis of staggered Arrays

Source: Internet
Author: User

C # arrays are worth learning. Here we mainly introduce C # staggered arrays, including one-dimensional arrays, two-dimensional arrays, and C # staggered arrays.

Arrays are often used in our programming. If you want to use arrays, this part will explain in detail how to define and use arrays in C.

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

One-dimensional array example: Copy codeThe Code is as follows: public static void Main ()
{
// One-dimensional array
Int [] inti = new int [3] {1, 2, 3 };
// Define an array inti and define that it can have only three elements. The values are 1, 2, and 3, respectively.
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 codeThe Code is as follows: public static void Main ()
{
// Two-dimensional array
Int [,] inti = new int [2, 3] {1, 2, 3}, {4, 5, 6 }};
// Define a two-dimensional array inti with two rows and three samples, and assign values to the inti array.
Inti [1, 0] = 5; // change inti [], that is, the second example. The value of the element in the first row is 5.
For (int I = 0; I <inti. GetLength (0); I ++)
// The inti. GetLength () method is used to obtain the number of elements in the specified dimension of the array.
{
For (int j = 0; j <inti. GetLength (1); j ++)
// The first for loop traverses the columns of the Two-dimensional array, and the second for loop traverses the rows of the Two-dimensional array.
{
Console. WriteLine (inti [I, j]);
// Print the value of the specified Element in the inti array.
}
}
}

Note: We can change the value in [2, 3], but in changing the number of rows and columns of a two-dimensional array, to increase the dimension, it can be expressed in [2, 3, 2.
C # staggered array:

Next, we will introduce a very personalized array called arrays in arrays (C # staggered arrays)

Copy codeThe Code is as follows: public static void Main ()
{
// Staggered Array
Int [] [] inti = new int [3] [];
// Unlike a one-dimensional or multi-dimensional array, the staggered array must have two [] numbers,
If you do not specify the initial identifier, you must specify the number of the first submark.
Inti [0] = new int [] {1, 2, 3}; // assign a value to the staggered Array
Inti [1] = new int [] {3, 4, 5, 6, 7 };
Inti [2] = new int [] {3, 4 };
For (int I = 0; I <inti. Length; I ++)
// Take the number of inti elements as the number of cycles
{
For (int j = 0; j <inti [I]. Length; j ++)
// Take the number of inti [I] elements as the number of cycles
{
Console. Write (inti [I] [j]);
// Print the staggered Array
}
Console. WriteLine ();
// Use the inti array element for line feed.
}
}

Note: we can see that the two-dimensional array and the C # staggered array print results are similar, but note that each array in the C # staggered array can be set to a fixed dimension, in addition, unlike two-dimensional arrays, columns have rows, and the element values of each array in the C # staggered array can be different.

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.