1. What are the definitions and important details of arrays?
Definition: An array is a group of data elements of the same type represented by a variable name. Each element is accessed by the variable name and one or more square brackets index names.
Note: 1> once an array is created, its size is fixed. C # dynamic arrays are not supported.
2> the index number of the array starts from 0. That is to say, if the dimension length is n, the index number ranges from 0 to n-1.
3> An array belongs to the reference type, that is, it is referenced on the stack or stack, but the array object itself is always on the stack.
2. What is the array category?
1> one-dimensional array
2> rectangular array (that is, all sub-arrays of a dimension have the same length)
3> staggered array s (that is, sub-arrays of different lengths can be included)
3. How to declare, initialize, and traverse array elements?
One-dimensional array:
Namespace array1
{
Class Program
{
Static void Main (string [] args)
{
// Int [] arr1; // declare an array
// Arr1 = new int [] {10, 20, 30, 40, 50}; // initialize the Array
Int [] arr1 = new int [] {10, 20, 30, 40, 50}; // The declaration and initialization can be completed in one step. Equivalent to: int [] arr1 = {10, 20, 30, 40, 50}; // shortcut syntax
// Var arr1 = new [] {10, 20, 30, 40, 50}; // an implicit array.
// Traverse the Array
// Method 1: Use the for Loop:
For (int I = 0; I <arr1.Length; I ++)
{
Console. WriteLine ("the value of the array element {0} is: {1}", I, arr1 [I]);
}
// Method 2: Use the foreach statement:
// Foreach (int item in arr1) // The iteration variable item indicates each element in the array.
//{
// Console. WriteLine ("element value: {0}", item );
//}
Console. ReadKey ();
}
}
}
The program output result is:
Two-dimensional array (rectangular array ):
Namespace array2
{
Class Program
{
Static void Main (string [] args)
{
Int [,] arr2 = new int [,] {0, 1, 2}, {10, 11, 12}; // declare and initialize a two-dimensional array.
// Traverse the Array
// Method 1: Use the for Loop:
// For (int I = 0; I <2; I ++)
//{
// For (int j = 0; j <3; j ++)
//{
// Console. WriteLine ("element [{0}, {1}] value: {2}", I, j, arr2 [I, j]);
//}
//}
// Method 2: Use the foreach statement:
Foreach (int item in arr2) // for multi-dimensional arrays, the element processing order is the rightmost index number that increases first. When the index is reduced from 0 to 1, the index on the next left is incremented, and the index on the right is reset to 0.
{
Console. WriteLine ("element values: {0}", item );
}
Console. ReadKey ();
}
}
}
The program output result is:
Staggered array:
Namespace arr3
{
Class Program
{
Static void Main (string [] args)
{
Int [] [] arr3 = new int [2] [];
/* The instantiation process of the staggered array is as follows:
First, instantiate the top-level array.
Next, instantiate each sub-array separately, and assign the reference value of the new array to the appropriate elements that contain the arrays.
*/
Arr3 [0] = new int [] {10, 11 };
Arr3 [1] = new int [] {12, 13, 14 };
// Traverse the array:
Foreach (int [] array in arr3)
{
Console. WriteLine ("Starting new array ");
Foreach (int item in array)
{
Console. WriteLine ("the elements of the array are: {0}", item );
}
}
Console. ReadKey ();
}
}
}
The program output result is:
4. Are some important attributes and methods of arrays summarized?
Namespace array4
{
Class Program
{
Static void Main (string [] args)
{
Int [] arr4 = new int [] {15, 20, 5, 25, 10 };
PrintArray (arr4 );
Console. WriteLine (); // line feed
Array. Sort (arr4); // method Sort, which is sorted in ascending order by default.
PrintArray (arr4 );
Console. WriteLine ();
Array. Reverse (arr4); // Method Reverse, Reverse
PrintArray (arr4 );
Console. WriteLine ();
Console. WriteLine ();
Console. WriteLine ("dimension: {0}, Length: {1}", arr4.Rank, arr4.Length); // attribute Rank indicates the array dimension, and attribute Length indicates the array Length.
Console. WriteLine ("GetLength (0) = {0}", arr4.GetLength (0); // The GetLength () method indicates the length of the specified dimension in the returned array.
Console. WriteLine ("GetType () = {0}", arr4.GetType (); // The GetType () method indicates the returned array type.
Console. ReadKey ();
}
Protected static void PrintArray (int [])
{
Foreach (int item in)
{
Console. Write ("{0}", item );
Console. Write (""); // Add a space after each array element.
}
}
}
}
The program output result is:
Ah, it's very late. I want to go to bed. Let's summarize it here. Please add some shortcomings!
The author's wheat forever.