Anda di halaman 1dari 13

Arrays

Collection of items of same Datatype


Types of Arrays
Simple Arrays
Multi Dimensional Arrays
Jagged Arrays

Simple Arrays
Array Declaration
An array is declared by defining the type of the elements inside
the array followed by empty brackets and a variable name
E.g.: int[] myArray;
Array Initialization
After declaring an array, memory must be allocated to hold all the
elements of the array.
An array is a reference type, so memory on the heap must be
allocated.
This is done by initializing the variable of the array using the new
operator with the type and the number of elements inside the
array.
Here you specify the size of the array.
E.g. myArray = new int[4];
Instead of using a separate line for the declaration and
initialization, you can declare and initialize an array in a single line

Array Initialization (Contd.)


int[] myArray = new int[4]; (OR)
int[] myArray = new int[4] {4, 7, 11, 2}; (OR)
int[] myArray = new int[] {4, 7, 11, 2}; (OR)
int[] myArray = {4, 7, 11, 2};
Accessing Array Elements
After an array is declared and initialized, you can access the array
elements using an indexer.
Arrays only support indexers that have integer parameters.
The indexer always starts with a value of 0 for the first element.
The highest number you can pass to the indexer is the number of
elements minus one, since the index starts at zero.
int[] myArray = new int[] {4, 7, 11, 2};
int v1 = myArray[0]; // read first element
int v2 = myArray[1]; // read second element
myArray[3] = 44; // change fourth element

Print the elements of the array


Length Property returns the no. of elements of the array
for (int i = 0; i < myArray.Length; i++)
{
Console.WriteLine(myArray[i]);
}
(OR)
foreach (int val in myArray)
{
Console.WriteLine(val);
}

Multidimensional Arrays
Ordinary arrays (also known as 1-dimensional arrays) are indexed
by a single integer. A multidimensional array is indexed by two or
more integers.
The following matrix shows the mathematical notation for a 2dimensional array that has three rows and three columns. The
first row has the values 1, 2 and 3, whereas the third row has the
values 7, 8 and 9.
123
456
789
Declaration & Initialization of Two Dimensional Array
int[,] twodim = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
Note: When using above array initializer, you must initialize every
element of the array. It is not possible to leave the initialization
for some values.

Declaration & Initialization of Three Dimensional Array


int[,,] threedim = { { { 1, 2 }, { 3, 4 } },
{ { 5, 6 }, { 7, 8 } },
{ { 9, 10 }, { 11, 12 } }
};
Printing the elements of Multi-Dimensional Arrays
// Loop to iterate through rows
for ( int i=0 ; i < twodim.GetLength(0) ; i ++)
{
// Loop to iterate throught columns
for ( int j=0 ; j < twodim.GetLength(1) ; j++)
{
Console.Write(twodim[i,j] + );
}
Console.WriteLine();
}

Jagged Arrays
Jagged Array is the kind of array which has array as its elements
Two Dimensional Array
Jagged Array
123

12

456
789

34567
8 9 10

A jagged array is more flexible in sizing the array.


With a jagged array every row can have a different size.
Declaration & Initialization of Jagged Array
A jagged array is declared by placing opening and closing brackets
after another.
With the initialization of the jagged array, first only the size that
defines the number of rows is set.
The second brackets that define the number of elements inside the
row are kept empty as every row has a different number of
elements

int[][] jagged = new int[3][];


jagged[0] = new int[2] { 1, 2 };
jagged[1] = new int[6] { 3, 4, 5, 6, 7, 8 };
jagged[2] = new int[3] { 9, 10, 11 };
Printing the elements of Jagged Array
for (int row = 0; row < jagged.Length; row++)
{
for (int element = 0; element < jagged[row].Length;element++)
{
Console.Write(jagged[row][element] + );
}
Console.WriteLine();
}

Array Class
Declaring an array with brackets is a C# notation of using the
Array class.
Using the C# syntax behind the scenes creates a new class that
derives from the abstract base class Array.
Properties
Property

Description

Length

The Length property returns the number of elements


inside the array. If the array is a multidimensional
array, you get the number of elements of all ranks. If
you need to know the number of elements within a
dimension, you can use the GetLength() method
instead.

LongLength

The Length property returns a int value, the


LongLength property returns the length in a long value

Rank

you get the number of dimensions of the array.

Creating Arrays
The Array class is abstract, so you cannot create an array by using
a constructor.
However, instead of using the C# syntax to create array instances,
it is also possible to create arrays by using the static
CreateInstance() method.
This is extremely useful if you dont know the type of the
elements in advance, as the type can be passed to the
CreateInstance() method as a Type object.
The following example shows how to create an array of type int
with a size of 5.
Array intArray1 = Array.CreateInstance(typeof(int), 5);
for (int i = 0; i < 5; i++)
{
intArray1.SetValue(33, i);
}

Sorting
The Array class implements a bubble-sort for sorting the elements in
the array.
The Sort() method requires the interface IComparable to be
implemented by the elements in the array.
string[] names = { "Christina", "Shakira", "Beyonce", "Gwen};
Array.Sort(names);
Reverse
Reverse Method is used to reverse the elements of the array
string[] names = { "Christina", "Shakira", "Beyonce", Gwen};
Array.Reverse(names);
Copying Arrays
The Clone() method that is defined with this interface creates a
shallow copy of the array.
int[] intArray2 = (int[])intArray1.Clone();

THANK YOU

Anda mungkin juga menyukai