Detailed explanation of the use of one dimensional array and multi dimensional array in C programming

  • 2021-09-05 00:42:22
  • OfStack

1-dimensional array
You can declare a 1-dimensional array of 5 integers as shown in the following example.


int[] array = new int[5];

This array contains elements from array [0] to array [4]. The new operator is used to create arrays and initialize array elements to their default values. In this example, all array elements are initialized to zero.
You can declare an array storing string elements in the same way. For example:


string[] stringArray = new string[6];

Array initialization
You can initialize an array when you declare it, in which case you do not need a level descriptor, because the level descriptor is already provided by the number of elements in the initialization list. For example:


int[] array1 = new int[] { 1, 3, 5, 7, 9 };

You can initialize string arrays in the same way. The following is a declaration of an array of strings, where each array element is initialized with the name of each day:


string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

If you initialize an array when you declare it, you can use the following shortcuts:


int[] array2 = { 1, 3, 5, 7, 9 };
string[] weekDays2 = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

You can declare an array variable without initializing it, but you must use the new operator when assigning an array to this variable. For example:


int[] array3;
array3 = new int[] { 1, 3, 5, 7, 9 }; // OK
//array3 = {1, 3, 5, 7, 9}; // Error

Arrays of Value Types and Arrays of Reference Types
Look at the following array declaration:


SomeType[] array4 = new SomeType[10];

The result of this statement depends on whether SomeType is a value type or a reference type. If it is a value type, the statement creates an array of 10 elements, each of which has an SomeType type. If SomeType is a reference type, the statement creates an array of 10 elements, each of which is initialized to a null reference.


Multidimensional array
Arrays can have multiple dimensions. For example, the following declaration creates a 2-dimensional array with 4 rows and 2 columns.


int[,] array = new int[4, 2];

The following declaration creates a 3-dimensional (4, 2, and 3) array.


int[, ,] array1 = new int[4, 2, 3];

Array initialization
You can initialize an array when you declare it, as shown in the following example.


// Two-dimensional array.
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// The same array with dimensions specified.
int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// A similar array with string elements.
string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" },
          { "five", "six" } };

// Three-dimensional array.
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
         { { 7, 8, 9 }, { 10, 11, 12 } } };
// The same array with dimensions specified.
int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
          { { 7, 8, 9 }, { 10, 11, 12 } } };

// Accessing array elements.
System.Console.WriteLine(array2D[0, 0]);
System.Console.WriteLine(array2D[0, 1]);
System.Console.WriteLine(array2D[1, 0]);
System.Console.WriteLine(array2D[1, 1]);
System.Console.WriteLine(array2D[3, 0]);
System.Console.WriteLine(array2Db[1, 0]);
System.Console.WriteLine(array3Da[1, 0, 1]);
System.Console.WriteLine(array3D[1, 1, 2]);

// Getting the total count of elements or the length of a given dimension.
var allLength = array3D.Length;
var total = 1;
for (int i = 0; i < array3D.Rank; i++) {
 total *= array3D.GetLength(i);
}
System.Console.WriteLine("{0} equals {1}", allLength, total);

Output:


string[] stringArray = new string[6];
0

You can also initialize an array without specifying a level.


string[] stringArray = new string[6];
1 If you choose to declare an array variable without initializing it, you must use the new operator to assign an array to the variable. The following example shows the use of new.

string[] stringArray = new string[6];
2

The following example assigns values to specific array elements.


array5[2, 1] = 25;

Similarly, the following example gets the value of a specific array element and assigns it to the variable elementValue.


string[] stringArray = new string[6];
4

The following code example initializes array elements to their default values (except for jagged arrays):


string[] stringArray = new string[6];
5


Related articles: