c sharp array

  • 2020-05-05 11:49:23
  • OfStack

An array is a data structure that is declared as

type[] arrayName;

Arrays have the following properties:

1. Arrays can be one-dimensional, multidimensional, or interlaced.
      2. The default value of the numeric array element is set to zero, and the default value of the reference element is set to null.
3. A jagged array is an array of arrays, so its elements are reference types, initialized as null.
      4. Array index from zero: an array with n elements is indexed from 0 to n-1.
5. Array elements can be of any type, including array types.

one-dimensional array

 


// Declare a one-dimensional array, uninitialized, equal to null 
  int[] intArray1; 
  // Initializes a declared one-dimensional array  
  intArray1 = new int[3]; // The default value of an array element is 0 
  intArray1 = new int[3]{1,2,3}; 
  intArray1 = new int[]{1,2,3}; 
  

  // Declare a one-dimensional array and initialize it  
  int[] intArray2 = new int[3]{1,2,3}; 
  int[] intArray3 = new int[]{4,3,2,1}; 
  int[] intArray4 = {1,2,3,4}; 
  string[] strArray1 = new string[]{"One","Two","Three"}; 
  string[] strArray2 = {"This","is","an","string","Array"}; 

The multidimensional array    


 // Declares a two-dimensional array, not initialized  
  short[,] sArray1; 
  // Initializes a declared two-dimensional array  
  sArray1 = new short[2,2]; 
  sArray1 = new short[2,2]{{1,1},{2,2}}; 
  sArray1 = new short[,]{{1,2,3},{4,5,6}}; 
  
  // Declare a two-dimensional array and initialize it at the same time  
  short[,] sArray2 = new short [1,1]{{100}}; 
  short[,] sArray3 = new short [,]{{1,2},{3,4},{5,6}}; 
  short[,] sArray4 = {{1,1,1},{2,2,2}}; 
  // Declare a 3d array and initialize it at the same time  
  byte[,,] bArray1 = {{{1,2},{3,4}},{{5,6},{7,8}}}; 


The jagged array

   


 // Declares a jagged array, not initialized  
  int[][] JagIntArray1; 
  // Initializes the declared jagged array  
  JagIntArray1 = new int [2][] { 
      new int[]{1,2}, 
      new int[]{3,4,5,6} 
     }; 
  JagIntArray1 = new int [][]{ 
       new int[]{1,2}, 
      // new int []{3,4,5}, 
       intArray2 // use int[] An array variable  
      }; 
  // Declares a jagged array and initializes it  
  int[][] JagIntArray2 = { 
      new int[]{1,1,1}, 
      //new int []{2,2}, 
      intArray1 
      }; 

An array is a group of elements in a data set of the same type, with a common name, accessed by an assigned index.

An array is a set of data of the same type. When accessing data in an array, it can be indicated by subscripts. The array elements in c# can be of any data type. The index of the array starts at 0, that is, the index corresponding to the first element is 0, and then increases one by one. Arrays can be one-dimensional or multidimensional.

One dimensional arrays are the most basic array types and are declared as

Data type [] array name;

Example:
int [] anArray; // declares an integer one-dimensional array

An array with two dimensions is a two-dimensional array, declared as
Data type [,] array name;
Example:

int [,] anArray; // declares an integer two-dimensional array
float [and] anArrayOfFloats; // declares a floating point two-dimensional array
string [,] anArrayOfStrings; // declares a two-dimensional array

as a string

When you declare an array variable, you haven't created the array yet, and you haven't allocated any memory for the elements in the array, so after you declare the array, you need to instantiate the array:

anArray = new int [2,4] ;
anArrayOfStrings = new stirng [2,4] ;

We can also initialize an array element with a given value.

int [, ] anArray = new int [2, 4] {{1,2,3,4},{5,6,7,8}};
string [and] anArrayOfStrings = new string [2, 2] {{" a ", "a second"}, {" title ", "second"}};

The following shortcut is also available:

int [, ] anArray = {{0,1,2,3},{1,2,3,4}};
string [and] anArrayOfStrings = {{" a ", "a second"}, {" title ", "second"}};

In C#, arrays provide us with some useful features that allow us to perform some more advanced functions.
Array name.Length: returns an integer representing the total number of elements in all dimensions of the array.
Array name.Rank: returns an integer representing the dimension of the array.
Array name.GetLength (int dimension) : returns an integer representing the number of elements in the specified dimension of the array (specified by the dimension parameter, starting from zero).

4. The foreach statement loops through the embedded statement for each element in the array or collection.
The syntax of the foreach statement is
foreach (data type identifier in expression)
Embed statement

// a one-dimensional array of integers with six elements;
int [] mf1 = new int [6]. // note the scope of the initialization array, or specify the initial value;

// an array of six one-dimensional integers with the initial values 1,2,3,4,5,6
int[] mf2=new int[6]{1,2,3,4,5,6};
 
// one-dimensional string array, if an initializer is provided, new operator
can also be omitted string[] mf3={"c","c++","c#"};

// one-dimensional object array
Object[] mf4 = new Object[5] { 26, 27, 28, 29, 30 };
 
// 2d integer array, initial value mf5[0,0]=1,mf5[0,1]=2,mf5[1,0]=3,mf5[1,1]=4
int[,] mf5=new int[,]{{1,2},{3,4}};
 
//6*6 two-dimensional integer array
int[,] mf6=new mf[6,6];

Let's look at traversing

for a one-dimensional array of strings

using System; 
public class MikeCat 
{ 
static void PrintArray(string[] arr) 
{ 
// Print array elements, arr.Length  Represents the number of elements in an array  
for(int i=0;i<arr.Length;i++) 
{ 
Console.WriteLine("arr[{0}]={1}",i,arr[i]); 
} 
} 
public static void Main() 
{ 
string[] arr={"c","c++","c#"}; 
// Pass the array as an argument  
PrintArray(arr); 
} 
} 

Program result: arr[0]=c arr[1]=c++ arr[2]=c#

Let's look at the traversal of a 4-row, 2-column (4*2) integer array:


using System; 
public class MikeCat 
{ 
static void PrintArray(int[,] arr) 
{ 
// Through the two FOR Loop through a two-dimensional array  
for(int i=0;i<4;i++)// Initialize the i As a loop variable ,i++ The self - increment operation of this variable is realized.  
//for After the loop satisfies the condition, the loop body is executed once i++ And then go to the next loop. simple c Grammar, here is a brief introduction for beginners to learn. ( For details, please refer to the c#  Advanced programming 4.0 " ) 
{ 
for(int j=0;j<2;j++) 
{ 
Console.WriteLine("arr[{0},{1}]={2}",i,j,arr[i,j]);// Print each element of a two-dimensional array  
} 
} 
} 
public static void Main() 
{ 
// The main function  
// Pass the array as an argument  
PrintArray(new int[,]{{1,2},{3,4},{5,6},{7,8}}; 
} 
} 


arr[0,0]=1 arr[0,1]=2 arr[1,0]=3 arr[1,1]= 5 arr[2,1]=6 arr[3,0]=7 arr[3,1]=8


Related articles: