A brief analysis of C interleaved arrays

  • 2020-05-07 20:15:02
  • OfStack

C# array has a lot to learn, here we mainly introduce C# jagged array, including 1 - dimensional array example, 2 - dimensional array example, C# jagged array and so on.

Arrays are often used in our programming, and since we all have a concept of arrays, this section will explain in detail how arrays are defined and used in C#.

Definition: an array is a set of values of the same type. An array is a reference type and therefore stored in the memory heap. The values of the elements in an array can be assigned when the array is defined, or individual elements can be assigned after the array is defined.

1-dimensional array example:
 
public static void Main() 
{ 
//1 Dimensional array  
int[] inti = new int[3] { 1, 2, 3 }; 
// define 1 An array inti And define that it can only have 3 An element , Values, respectively 1,2,3 
inti[1] = 4;// change inti[1] That is the first 2 The value of each element is zero 4. 
Console.WriteLine(inti[0], inti[1], inti[2]); 
} 

2-dimensional array example:
 
public static void Main() 
{ 
//2 Dimensional array  
int[,] inti = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } }; 
// define 1 a 2 line 3 Cases of 2 Dimensional array inti, And assign a value to it . 
inti[1, 0] = 5;// change inti[1,0] That is the first 2 case , The first 1 The value of the element of the row is 5. 
for (int i = 0; i < inti.GetLength(0); i++) 
//inti.GetLength() Method to get the number of elements of the specified dimension for an array . 
{ 
for (int j = 0; j < inti.GetLength(1); j++) 
// The first 1 a for To iterate over 2 The column of the dimension array , The first 2 a for To iterate over 2 The rows of the dimension array . 
{ 
Console.WriteLine(inti[i, j]); 
// print inti The value of the specified element in the array . 
} 
} 
} 

Note: we can change the values in [2,3], only the number of rows and columns in the 2-dimensional array.
C# jagged array:

Next, we introduce a very personalized array called an array within an array (C# jagged array)

 
public static void Main() 
{ 
// Jagged arrays  
int[][] inti = new int[3][]; 
// with 1 Dimensional or multidimensional arrays are different , A jagged array needs to be defined 2 a [] No. , 
 Without specifying an initial pointer , Must specify the 1 The number of subscripts . 
inti[0] = new int[] { 1, 2, 3 };// Assign a value 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++) 
// In order to inti Is the number of cycles  
{ 
for (int j = 0; j < inti[i].Length; j++) 
// In order to inti[i] Is the number of cycles  
{ 
Console.Write(inti[i][j]); 
// Print jagged array  
} 
Console.WriteLine(); 
// In order to inti The array element of . 
} 
} 


Note: we see that this 2-dimensional array is similar to the C# jagged array printed, but it is important to note that each array inside C# jagged array is dimensioning, and that unlike 2-dimensional arrays that have columns and rows, each array in C# jagged array can have different values.

Related articles: