Declaration and traversal of the jagged array of C # (array of arrays)

Source: Internet
Author: User

1. What is a jagged array?

The number of elements in each row in the array is different.

2. Declare a jagged array.

Declares an array of arrays whose syntax is primarily in the declaration of an array of square brackets, such as:

Int[][] Arrayname;

The first pair of parentheses sets the number of rows in the array, and the second pair defines the number of elements of each row, first set to null. Because the number of elements contained in each row is not equal.

There are also the following statements:

Initializes an array (also known as a subarray) that contains the other arrays, and then initializes the sub-arrays.

   New int [2] []   ; New int [3];    New int [4];

Improved form of literal value assignment:

New int [3newint[] {1,2,3},newint{1 New int {4,5}};

You can also simplify the array by placing the initial values and declarations on the same line as follows:

int [] Arrayname = {newint[] {1,2,3newint{1 New int {4,5}};

3. The traversal of the jagged array can be used for loops and foreach methods.

For loop: Use two for loop array for one by one times calendar, see the code for details.

foreach: A nested method is also used to get the actual data.

Why nest it? Because the array myIntArray4 contains the int[] element, not the int element, the subarray and the array itself must be looped.

 Static voidMain (string[] args) {            //declares a jagged array of three rows            int[] myIntArray4; MyIntArray4=New int[3][]; myintarray4[0] =New int[] {1, One,111 }; myintarray4[1] =New int[] {2, A }; myintarray4[2] =New int[] {3, -,333,3333 };  for(inti =0; i < myintarray4.length; i++)            {                 for(intj =0; J < Myintarray4[i]. Length; J + +) {Console.WriteLine ("{0}", Myintarray4[i][j]); }}/*for Loop * /foreach(int[] AbinchMyIntArray4) {               foreach(intAbcinchAB)              {Console.WriteLine (ABC);        }/*foreach*/} console.read (); }

Declaration and traversal of the jagged array of C # (array of arrays)

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.