|
|
| Single-Dimensional
Arrays |
|
You can declare an array of five integers as in the
following example:
int[] myArray = new int [5];
This array contains the elements from myArray[0] to
myArray[4]. The new operator is used to create the array and initialize the
array elements to their default values. In this example, all the array elements
are initialized to zero. An array that stores string elements can be declared
in the same way.
For example:
string[] myStringArray = new string[6];
|
| Array Initialization |
|
It is possible to initialize an array upon
declaration, in which case, the rank specifier is not needed because it is
already supplied by the number of elements in the initialization list. For
example:
int[] myArray = new int[] {1, 3, 5, 7, 9};
A string array can be initialized in the same way. The
following is a declaration of a string array where each array element is
initialized by a name of a day:
string[] weekDays = new string[]
{"Sun","Sat","Mon","Tue","Wed","Thu","Fri"};
When you initialize an array upon declaration,
it is possible to use the following shortcuts:
int[] myArray = {1, 3, 5, 7, 9};
string[] weekDays =
{"Sun","Sat","Mon","Tue","Wed","Thu","Fri"};
It is possible to declare an array variable without
initialization, but you must use the new operator when you assign an array to
this variable. For example:
int[] myArray;
myArray = new int[] {1, 3, 5, 7, 9}; // OK
myArray = {1, 3, 5, 7, 9}; // Error
|
| Value Type and Reference Type
Arrays |
|
Consider the following array declaration:
MyType[] myArray = new MyType[10];
The result of this statement depends on whether MyType
is a value type or a reference type. If it is a value type, the statement
results in creating an array of 10 instances of the type MyType. If MyType is a
reference type, the statement creates an array of 10 elements, each of which is
initialized to a null reference.
|
| Passing Arrays as Parameters |
|
You can pass an initialized array to a method. For
example:
PrintArray(myArray); You can also initialize and pass
a new array in one step. For example:
PrintArray(new int[] {1, 3, 5, 7, 9}); Example
In the following example, a string array is
initialized and passed as a parameter to the PrintArray method, where its
elements are displayed:
// cs_sd_arrays.cs using System;
public class ArrayClass
{
static void PrintArray(string[] w)
{
for (int i = 0 ; i < w.Length ; i++)
Console.Write(w[i] + "{0}", i < w.Length - 1 ? " "
: "");
Console.WriteLine();
}
public static void Main()
{
// Declare and initialize an array:
string[] WeekDays = new string []
{"Sun","Sat","Mon","Tue","Wed","Thu","Fri"};
// Pass the array as a parameter:
PrintArray(WeekDays);
}
}
Output
Sun Sat Mon Tue Wed Thu Fri
|
|
| Multidimensional
Arrays |
|
Arrays can have more than one dimension. For example,
the following declaration creates a two-dimensional
array of four rows and two columns:
int[,] myArray = new int[4,2];
Also, the following declaration creates an array of
three dimensions, 4, 2, and 3:
int[,,] myArray = new int [4,2,3];
Array Initialization
You can initialize the array upon declaration as shown
in the following example:
int[,] myArray = new int[,] {{1,2}, {3,4}, {5,6},
{7,8}};
You can also initialize the array without specifying
the rank:
int[,] myArray = {{1,2}, {3,4}, {5,6}, {7,8}};
If you choose to declare an array variable without
initialization, you must use the new operator to assign an array to the
variable.
For example:
int[,] myArray; myArray = new int[,] {{1,2}, {3,4},
{5,6}, {7,8}}; // OK
myArray = {{1,2}, {3,4}, {5,6}, {7,8}}; // Error
You can also assign a value to an array element,
for example:
myArray[2,1] = 25;
|
| Passing Arrays as Parameters |
|
You can pass an initialized array to a method. For
example:
PrintArray(myArray);
You can also initialize and pass a new array in one
step. For example:
PrintArray(new int[,] {{1,2}, {3,4}, {5,6}, {7,8}});
Example
In this example, a two-dimensional array is
initialized and passed to the PrintArray method, where its elements are
displayed.
// cs_td_arrays.cs
using System;
public class ArrayClass
{
static void PrintArray(int[,] w)
{
// Display the array elements:
for (int i=0; i < 4; i++)
for (int j=0; j < 2; j++)
Console.WriteLine("Element({0},{1})={2}", i, j, w[i,j]);
}
public static void Main()
{
// Pass the array as a parameter:
PrintArray(new int[,] {{1,2}, {3,4}, {5,6}, {7,8}});
}
}
Output
Element(0,0)=1
Element(0,1)=2
Element(1,0)=3
Element(1,1)=4
Element(2,0)=5
Element(2,1)=6
Element(3,0)=7
Element(3,1)=8
|
| Jagged Arrays
|
|
A jagged array is an array whose elements are arrays.
The elements of a jagged array can be of different dimensions and sizes. A
jagged array is sometimes called an "array-of-arrays." This topic contains
examples of declaring, initializing, and accessing jagged arrays. The following
is a declaration of a single-dimensional array that has three elements, each of
which is a single-dimensional array of integers:
int[][] myJaggedArray = new int[3][];
Before you can use myJaggedArray, its elements must be
initialized. You can initialize the elements like this example:
myJaggedArray[0] = new int[5];
myJaggedArray[1] = new int[4];
myJaggedArray[2] = new int[2];
Each of the elements is a single-dimensional array of
integers. The first element is an array of 5 integers, the second is an array
of 4 integers, and the third is an array of 2 integers. It is also possible to
use initializers to fill the array elements with values, in which case you
don't need the array size, for example:
myJaggedArray[0] = new int[] {1,3,5,7,9};
myJaggedArray[1] = new int[] {0,2,4,6};
myJaggedArray[2] = new int[] {11,22};
You can also initialize the array upon declaration
like this:
int[][] myJaggedArray = new int [][]
{ new int[] {1,3,5,7,9},
new int[] {0,2,4,6},
new int[] {11,22} };
You can use the following shortcut (notice that you
cannot omit the new operator from the elements initialization because there is
no default initialization for the elements):
int[][] myJaggedArray = {
new int[] {1,3,5,7,9},
new int[] {0,2,4,6},
new int[] {11,22}
};
You can access individual array elements like these
examples: // Assign 33 to the second element of the first array:
myJaggedArray[0][1] = 33;
// Assign 44 to the second element of the third
array:
myJaggedArray[2][1] = 44;
It is possible to mix jagged and multidimensional
arrays. The following is a declaration and initialization of a
single-dimensional jagged array that contains two-dimensional array elements of
different sizes:
int[][,] myJaggedArray = new int [3][,] {
new int[,] { {1,3}, {5,7} },
new int[,] { {0,2}, {4,6}, {8,10} },
new int[,] { {11,22}, {99,88}, {0,9} }
};
You can access individual elements like this example,
which displays the value of the element [1,0] of the first array (value 5):
Console.Write("{0}", myJaggedArray[0][1,0]);
Example This example builds an array, myArray, whose
elements are arrays. Each one of the array elements has a different size.
// cs_array_of_arrays.cs
using System;
public class ArrayTest
{
public static void Main()
{
// Declare the array of two elements:
int[][] myArray = new int[2][];
// Initialize the elements: myArray[0] = new int[5]
{1,3,5,7,9};
myArray[1] = new int[4] {2,4,6,8};
// Display the array elements:
for (int i=0; i < myArray.Length; i++)
{
Console.Write("Element({0}): ", i);
for (int j = 0 ; j < myArray[i].Length ; j++)
Console.Write("{0}{1}", myArray[i][j], j
== (myArray[i].Length-1) ? "" : " ");
Console.WriteLine();
}
}
}
Output
Element(0): 1 3 5 7 9
Element(1): 2 4 6 8
|
|