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)