int[] num = new int[5] { 1, 2, 3, 4, 5 };
Console.WriteLine(num[0]); //---1---
Console.WriteLine(num[1]); //---2---
Console.WriteLine(num[2]); //---3---
Console.WriteLine(num[3]); //---4---
Console.WriteLine(num[4]); //---5---
The index of an array starts from 0 tonum
has size of 5 so the index runs from 0 to 4.
You usually use a loop construct to run through the elements in an array. For example, you can use the for statement to iterate through the elements of an array:
for (int i = 0; i < num.Length; i++)
Console.WriteLine(num[i]);
You can also use the foreach
statement, which is a clean way to iterate through the elements of an array quickly:
foreach (int n in num)
Console.WriteLine(n);
Multidimensional Arrays
So far the arrays you have seen are all one-dimensional ones. Arrays may also be multidimensional. To declare a multidimensional array, you can the comma (,
) separator. The following declares xy to be a 2-dimensional array:
int[,] xy;
To initialize the two-dimensional array, you use the new keyword together with the size of the array:
xy = new int[3,2];
With this statement, xy can now contain six elements (three rows and two columns). To initialize xy with some values, you can use the following statement:
xy = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
The following statement declares a three-dimensional array:
int[, ,] xyz;
To initialize it, you again use the new keyword together with the size of the array:
xyz = new int[2, 2, 2];
To initialize the array with some values, you can use the following:
int[, ,] xyz;
xyz = new int[,,] {
{ { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } }
};
To access all the elements in the three-dimensional array, you can use the following code snippet:
for (int x = xyz.GetLowerBound(0); x <= xyz.GetUpperBound(0); x++)
for (int y = xyz.GetLowerBound(1); y <= xyz.GetUpperBound(1); y++)
for (int z = xyz.GetLowerBound(2); z <= xyz.GetUpperBound(2); z++)
Console.WriteLine(xyz[x, y, z]);
The Array
abstract base class contains the GetLowerBound()
and GetUpperBound()
methods to let you know the size of an array. Both methods take in a single parameter, which indicates the dimension of the array about which you are inquiring. For example, GetUpperBound(0)
returns the size of the first dimension, GetUpperBound(1)
returns the size of the second dimension, and so on.
You can also use the foreach statement to access all the elements in a multidimensional array:
foreach (int n in xyz)
Console.WriteLine(n);
These statements print out the following:
1
2
3
4
5
6
7
8
Arrays of Arrays: Jagged Arrays
An array's elements can also contain arrays. An array of arrays is known as a
Point[][] lines = new Point[5][];
lines[0] = new Point[4];
lines[1] = new Point[15];
lines[2] = new Point[7];
lines[3] = ...
lines[4] = ...
Here, lines
is a jagged array. It has five elements and each element is a Point
array. The first element is an array containing four elements, the second contains 15 elements, and so on.
The Point
class represents an ordered pair of integer x- and y-coordinates that defines a point in a two-dimensional plane.
You can use the array initializer to initialize the individual array within the lines array, like this:
Point[][] lines = new Point[3][];
lines[0] = new Point[] {
new Point(2, 3), new Point(4, 5)
}; //---2 points in lines[0]---
lines[1] = new Point[] {
new Point(2, 3), new Point(4, 5), new Point(6, 9)
}; //---3 points in lines[1]----
lines[2] = new Point[] {
new Point(2, 3)
}; //---1 point in lines[2]---
To access the individual Point
objects in the lines array, you first specify which Point
array you want to access, followed by the index for the elements in the Point array, like this:
//---get the first point in lines[0]---
Point ptA = lines[0][0]; //----(2,3)
//---get the third point in lines[1]---
Point ptB = lines[1][2]; //---(6,9)---