2.
An array is a collection of variables of the same type
that are referred to by a common name
arrays can have one or more dimensions, although
the one-dimensional array is the
most common
ARRAY
3.
A one-dimensional array is a list of related variables.
Such lists are common in programming.
For example, you might use a one-dimensional array
to store the account numbers of the
active users on a network. Another array might store
the current batting averages for a
baseball team.
type[ ] array-name = new type[size];
For example:
int arr = new int[100]
One-Dimensional
Arrays
4.
If you come from a C or C++ background, pay special
attention to the way arrays are declared. Specifically, the
square brackets follow the type name, not the array name.
Here is an example. The following creates an int
array of ten elements and links it to an
array reference variable named sample.
int[] sample = new int[10];
One-Dimensional
Arrays
5.
class ArrayDemo {
static void Main() {
int[] sample = new int[10];
int i;
for(i = 0; i < 3; i = i+1)
sample[i] = i;
for(i = 0; i <3; i = i+1)
Console.WriteLine("sample[" + i + "]: " + sample[i]);
}
}
sample[0]: 0
sample[1]: 1
sample[2]: 2
Code One Dimensional
Array
6.
Arrays can be initialized when they are created.
general form for initializing a onedimensional array
type[ ] array-name = { val1, val2, val3, ..., valN };
Initialized One
Dimensional array
7.
The simplest form of the multidimensional array is
the 2-dimensional array. A 2-dimensional array is a
list of one-dimensional arrays.
string [,] names
Pay careful attention to the declaration. Notice that
the two dimensions are separated from
each other by a comma. In the first part of the
declaration, the syntax
[,]
Two-Dimensional
Arrays
8.
int [,] a = new int [3,4]
{
{0, 1, 2, 3} , /* initializers for row indexed by 0 */ {4,
5, 6, 7} , /* initializers for row indexed by 1 */ {8, 9,
10, 11} /* initializers for row indexed by 2 */
};
Initialized Two
Dimensional Array
9.
class TwoD
{
static void Main()
{
int t, i;
int[,] table = new int[3, 4];
for(t=0; t < 3; ++t)
{
for(i=0; i < 4; ++i)
{
table[t,i] = (t*4)+i+1;
Console.Write(table[t,i] + " ");
}
Console.WriteLine();
}
}
}
Code Two Dimensional
Array