Single dimension Array
An array always stores value of single data type placed
in contiguous memory locations and these values are reference using a common
array name.
These elements are accessed using subscripts or index
numbers that determine the position of the element in the array list.
C# array supports zero-based index values in an array.
This means that the first array element has index number zero and the last
element has index n-1, where n stand
for total number of array.
Syntax:
datatype[] arrayName;
arrayName=new Datatype[int(length of array)];
or
datatype[]
arrayName=new Datatype[int(length of array)];
Declaring
and Initializing Array
string[] studentsName;//
declaration of string array
Array initialize by the new
keyword when we specify the size of the array, the new keyword allocates memory
to the array.
studentsName = new string[4];// create string
array object
Or we can create and declare the array in one line
string[] studentsName= new
string[4];
Initialization
of array in one statement
string[] studentsName = new string[] {
"Ahsan","Faizan","Majid","Abdul
Ahad"
};
Console.WriteLine(studentsName[3]);//
it will print Abdul Ahad
Initialization
of array separately
string[] studentsName = new string[4];
studentsName[0] = "Ahsan";
studentsName[1] = "Faizan";
studentsName[2] = "Majid";
studentsName[3] = "Abdul
Ahad";
Console.WriteLine(studentsName[3]);//
it will print Abdul Ahad
Initialization of array at
runtime
Example:
string[] studentsName = new string[4];
Console.WriteLine("Please Enter
Students Name\n");
for (int i = 0; i < 4;
i++)
{
studentsName[i] = Console.ReadLine();
}
Console.WriteLine("\nList Of
Students is\n");
for (int i = 0; i < 4;
i++)
{
Console.WriteLine(studentsName[i]);
}
Output:
Multi Dimension Array
By the help of multi
Dimension Array we can store combination of values of single dimension in two
or more dimensions.
These dimensions are
represents by columns and rows like matrix or MS Excel sheet.
00
|
01
|
02
|
10
|
11
|
12
|
20
|
21
|
22
|
30
|
31
|
32
|
40
|
41
|
42
|
50
|
51
|
52
|
In matrix 00, 01, 02 …are
positions of rows and columns but in multi- dimension array these are indexes
of rows and columns.
There are two type of multi-
dimension array.
- Rectangular Array
- Jagged Array
Rectangular Array
Rectangular multidimensional
array have constant number of columns in each rows.
For example:
3X3, 2X2, 5X5 matrix
Syntax:
datatype[,] arrayName;
arrayName=new Datatype[int(no of rows), int(no of columns)];
or
datatype[,]
arrayName=new Datatype[int(no of rows), int(no of columns)]
Example:
int count = 0;
string[,] studentsName = new string[3,4];
//inserting values to array
for (int i = 0; i < 3;
i++)
{
for (int j = 0; j < 4;
j++)
{
studentsName[i, j] = count.ToString();
count++;
}
}
//printing values from array
for (int i = 0; i < 3;
i++)
{
for (int j = 0; j < 4;
j++)
{
Console.Write(studentsName[i, j]+" ");
}
Console.WriteLine();
}
Output:
0 1 2
3
4 5 6 7
8 9 10
11
Notice that there
are 3 rows but each row has 4 column.
Jagged Array
Jagged multidimensional array
may have different number of columns in each row.
For example:
Syntax:
datatype[][] arrayName;
arrayName=new Datatype[int(no of rows)][ ]
//we leave column [] empty because jagged array allow
us to increase/decrease number of columns in any rows
or
datatype[,]
arrayName=new Datatype[int(no of rows)][ ];
Example:
Coding:
string[][] classRoom = new string[4][];
classRoom[0]=new string[]{
"student1","student2","student3","student4","student5"
};
classRoom[1] = new string[]{
"student1","student2","student3"
};
classRoom[2] = new string[]{
"student1","student2","student3","student4"
};
classRoom[3] = new string[]{
"student1","student2"
};
for (int i = 0; i <
classRoom.GetLength(0); i++)
{
Console.WriteLine("student at
class room {0}",i+1);
for (int j = 0; j < classRoom[i].GetLength(0);
j++)
{
Console.Write(classRoom[i][j]+", ");
}
Console.WriteLine();
Console.WriteLine();
}
Output:
Properties
of Array
string[] mystring = new string[5];
Console.WriteLine(mystring.IsFixedSize);
// it return boolean
value(True or False), which indicate the size of array either it is fixed or
not
Console.WriteLine(mystring.IsReadOnly);
// it return boolean
value(True or False), which indicate the array either it is read only or not
Console.WriteLine(mystring.IsSynchronized);
//it return boolean value,
which indicate that array working is well or not while using by multiple
threads
Console.WriteLine(mystring.Length);
//it return length of array
in 32bit integer value
Console.WriteLine(mystring.LongLength);
//it return length of array
in 64bit integer value
Console.WriteLine(mystring.Rank);
//it return dimension of
array in our case it return 1
Console.WriteLine(mystring.SyncRoot);
// it return the object which
provide access to the array (it return System.String[])
Output:
True
False
False
5
5
1
System.String[]
Methods
of Array
CopyTo method copies all
the elements of current single dimension array to another single dimension
array.
Example:
string[] Companies = new string[]{
"Microsoft","Dell","Samsung"
};
string[] newCompanies=new string[3];
Companies.CopyTo(newCompanies,0);
foreach (var item in Companies)
{
Console.WriteLine(item);
}
Output:
Microsoft
Dell
Samsung
SetValue method allows you
to set the value to array at specific index.
Example:
string[] Companies = new string[]{
"Microsoft","Dell","Samsung"
};
Companies.SetValue("HP", 1);
foreach (var item in Companies)
{
Console.WriteLine(item);
}
Output:
Microsoft
HP
Samsung
GetLength method returns
number of element in the array.
Example:
Console.WriteLine(Companies.GetLength(0));
Output:
3
GetUpperBound method returns the
last index (upper bound) of the array.
Example:
Console.WriteLine(Companies.GetUpperBound(0));
Output:
2
Example:
Console.WriteLine(Companies.GetLowerBound(0));
Output:
0
Example:
Console.WriteLine(Companies.GetValue(0));
Output:
Microsoft
Array Class
Array class provides many
properties and methods for create and manipulate array.
Array Class Sort Method
Example:
string[] Companies = new string[]{
"Microsoft","Dell","Samsung"
};
Array.Sort(Companies);
foreach (var item in Companies)
{
Console.WriteLine(item);
}
Output:
Dell
Microsoft
Samsung
Array Class Clear Method
This method clears all the
element of the array.
Example:
string[] Companies = new string[]{
"Microsoft","Dell","Samsung"
};
Array.Clear(Companies, 0, 3);
foreach (var item in Companies)
{
Console.WriteLine(item);
}
Console.ReadLine();
Output:
Nothing print due to clear
method
Array Class Reverse Method
This method reverse all the
element of the array.
Example:
string[] Companies = new string[]{
"Microsoft","Dell","Samsung"
};
Array.Reverse(Companies);
foreach (var item in Companies)
{
Console.WriteLine(item);
}
Output:
Samsung
Dell
Microsoft
Array Class IndexOf Method
This method returns index of
specific element of the array.
Example:
string[] Companies = new string[]{
"Microsoft","Dell","Samsung"
};
Console.WriteLine(Array.IndexOf(Companies,"Dell"));
Output:
1
No comments:
Post a Comment