C # tutorial 013 Array
Array is a group of element, collected together to hold similar type of
values or object. Array is a kind of object which can hold multiple inbuilt
value or objects in its. Array comes under the namespace System.
Array has a property called index , Index help to access a particular
position of array. Array object can be access through looping also.
Example 1
int[]
student;
string[]
student1;
byte[]
student2;
bool[]
student3;
char[]
student4;
Array
is an object
which can be simple or complex depending upon declaration. We can
declare an array only, later we can initialised this array , and in
other
hand, we can declare and initialised in same statement also. Here is
the
example of array declaration. Array has its size, the below example.Here
the array size is 10, that means array can be define from 0 to 10.
This array can hold 11 elements in it.
int[]
student = new
int[10];
string[]
student1 = new
string[10];
byte[]
student2 = new
byte[10];
bool[]
student3 = new
bool[10];
char[]
student4 = new
char[10];
Array size is not fixed, it can
be redefined when necessary. The declaration is simple, here are some
example of re initialization of array size.
int[]
student = new
int[10];
string[]
student1 = new
string[10];
byte[]
student2 = new
byte[10];
bool[]
student3 = new
bool[10];
char[]
student4 = new
char[10];
student
= new
int[15];
student1
= new
string[15];
student2
= new
byte[15];
student3
= new
bool[15];
student4
= new
char[15];
Multidimensional Array : The previous example shows the examples of simple Linear array. Array can
have more than one dimension to hold more complex data structure.
int[]
student = new
int[10];//one
dimentional
int[,]
student1 = new
int[10,
10];//two
dimentioal
int[,
,] student2 = new
int[10,
10, 10];//three
dimentional
int[,
, ,] student3 = new
int[10,
10, 10,10];//four
dimentional
int[,]
student = new
int[2,
2] {
{0,
1} ,
{3,
5}
};
string[,]
student1 = new
string[2,
2] {
{"val1",
"val2"}
,
{"val3",
"val4"}
};
bool[,]
student2 = new
bool[3,
2] {
{true,
false}
,
{true,
true},
{false,false}
};
Rectangular Array: Rectangular array is with rectangular format. This
array have a length, and each index contain equal number of element
of same length. It is a tabular format of equal number of row and column . In
rectangular array number of row equal to the number of column each row.
The below example of multiple type of declaration of rectangular array.
It can be one dimensional, two dimensional or three dimensional.
int[,]
student = new
int[2,
2] {
{0,
1} ,
{3,
5}
};
string[,]
student1 = new
string[2,
2] {
{"val1",
"val2"}
,
{"val3",
"val4"}
};
bool[,]
student2 = new
bool[3,
3] {
{true,
false,false}
,
{true,
true,true},
{false,false,false}
};
Jagged Array : Jagged does not contain equal number of column in each row. It does not
guarantee that number of column would be same in each row.Rather is content
different column size in different row, it is a kind of array of arrays.
It can be defined with, parenthesis depending upon the requirement.
Below is the example of Jagged array.
string[][]
student = new
string[3][];
student[0]
= new
string[2];
student[1]
= new
string[3];
student[2]
= new
string[2];
student[0]
= new
string[]
{ "val1",
"val2"
};
student[1]
= new
string[]
{ "val3",
"val4",
"val5"};
student[2]
= new
string[]
{ "val6",
"val7"
};
Array Boundary : Array have is
its boundary, it can be upper or lower. We get upper and lower boundary of
and array by the method GetUpperBound() and GetLowerBound(). Below is
example of upper and lower boundary. The boundary tales the upper and
lower limit of Array size.
int[]
student = new
int[]
{ 1, 2, 3, 4, 5 };
int
lower_bound = student.GetLowerBound(0);
int
upper_bound = student.GetUpperBound(0);
Console.WriteLine(lower_bound);
Console.WriteLine(upper_bound);
//Output
:0
//Output
:4
Looping through Array : We can you access
array data by the help of loop. We can read its each object of an array
loop statement. Depending upon the dimension of the array , number of the
loop required. For a single linear loop, a single loop is required, for a
two dimensional array ,two loop is required. Here is the example of loop
statement true array.
Example 1
Example 1
int[]
student = new
int[]
{ 1, 2, 3, 4, 5 };
for
(int
i = 0; i < student.Length; i++)
{
Console.WriteLine(student[i]);
}
//Output
:1
//Output
:2
//Output
:3
//Output
:4
//Output
:5
Example 2
string[,]
student = new
string[2,
2] { { "val1",
"val2"
}, { "val3",
"val4"
} };
for
(int
i = 0; i < student.GetLength(0); i++)
{
for
(int
j = 0; j < student.GetLength(1); j++)
{
string
GetVal = student[i, j].ToString();
Console.WriteLine(GetVal.ToString());
}
}
}
//Output
:val1
//Output
:val2
//Output
:val3
//Output
:val4
foreach Loop : foreach loop also applied on
array to access array element. From a simple Linear array to a complex
multidimensional array, for each loop works.
Example 1
foreach (int i in student)
Example 1
int[]
student = new
int[]
{ 1, 2, 3, 4, 5 };
foreach (int i in student)
{
Console.WriteLin(i.ToString());
}
//Output
:1
//Output
:2
//Output
:3
//Output
:4
//Output
:5
Passing Array as Argument
: Array can be pass as an argument of a function or method. This
technique can be off two type, pass by value or pass by reference. When
pass by value is used, the entire value is copied to the method variable.
Only copy is done from original variable to the method variable.
Original variable remain unaffected. When pass by Reference is used, the
original memory location of the the variable is passed through the
method. If any value change in the method, the original
variable will be affected. Here are the example of pass by value and
pass by reference.
Example 1
Example2
Example 1
string[,]
student = new
string[2,
2] { { "val1",
"val2"
}, { "val3",
"val4"
} };
double
CalCulateFees(string[,]
param1)
{
return
0.0;
}
string[,]
student = new
string[2,
2] { { "val1",
"val2"
}, { "val3",
"val4"
} };
double
CalCulateFees(ref
string[,]
param1)
{
return
0.0;
}
Return Array : A function on Method can return
array. The signature of the function on method will be the return type
of that array. Here is the example of function return array.
public
string[,]
CalCulateFees( )
{
string[,]
student = new
string[2,
2] { { "val1",
"val2"
}, { "val3",
"val4"
} };
return
student;
}
Array value
assignment: array value can be a sign by simple operator. Another way to
assign array value by using set value method. Hear that the example of
array value array value assignment.
Example 1
int[]
student = new
int[]
{ 1, 2, 3, 4, 5 };
student[2]
= 99;
Console.WriteLine(student[2]);
Example 2
string[,]
student = new string[2,
2] { { "val1", "val2"
}, { "val3", "val4"
} };
for
(int
i = 0; i < student.GetLength(0); i++)
{
for
(int
j = 0; j < student.GetLength(1); j++)
{
if
(i==1 && j==1)
student[i,
j] = "Hellow
World";
}
};
Console.WriteLine(student[1,
1]);
//Output
:Hellow World
Array is a
base class which comes under the namespace system. Array class
contain a number of property and method to make development speedy
and easier. Array base class content almost all useful method for array
manipulation and development. Here is the most useful properties and
methods of array class which use is very common.
Properties
IsFixedSize : Returns a value , indicating whether the Array has a fixed size.
IsReadOnly : Returns a value indicating whether the Array is read-only.
IsFixedSize : Returns a value , indicating whether the Array has a fixed size.
int[]
student = new
int[2];
Console.WriteLine(student.IsFixedSize);
//Output :True
IsReadOnly : Returns a value indicating whether the Array is read-only.
int[]
student = new
int[2];
Console.WriteLine(student.IsReadOnly);
//Output :False
Length : Returns the total number of elements in all the dimensions of the Array.
Rank : Returns the rank (number of dimensions) of the Array. For example, a one- dimensional array returns 1, a two-dimensional array returns 2, and so on.
Methods
GetValue : Returns value at the specified position in the multidimensional Array
SetValue :Sets a value to the element at the specified position in the multidimensional Array
int[]
student = new
int[2];
Console.WriteLine(student.Length);
//Output :2
Rank : Returns the rank (number of dimensions) of the Array. For example, a one- dimensional array returns 1, a two-dimensional array returns 2, and so on.
int[]
student = new
int[2];
Console.WriteLine(student.Rank);
//Output :1
Methods
GetValue : Returns value at the specified position in the multidimensional Array
string[,]
student = new
string[2,
2] { { "val1",
"val2"
}, { "val3",
"val4"
} };
Console.WriteLine(student.GetValue(1,
1).ToString());
//Output :val4
SetValue :Sets a value to the element at the specified position in the multidimensional Array
string[]
student = new
string[]
{ "1",
"2",
"3",
"4",
"5"
};
student.SetValue("Hellow
World",0);
Console.WriteLine(student[0]);
///Output :Hellow
World
Sort :Sorts the elements in an entire one-dimensional Array.
Copy :Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element.
Reverse : Reverses the sequence of the elements in the entire one-dimensional Array.
ToString() Returns a string that represents the current object
GetUpperBound : Gets the index of the last element of the specified dimension in the array.
GetLowerBound : Returns the index of the first element of the specified dimension in the array.
ForEach : For each loop through loop.
int[]
student = new
int[5]
{ 99, 66, 77, 11, 22 };
Array.Sort(student);
//
write array
foreach
(int
i in
student)
Response.Write(i
+ "
");
//
output: 11
//
output: 22
//
output: 66
//
output: 77
//
output: 99
Copy :Copies a range of elements from an Array starting at the first element and pastes them into another Array starting at the first element.
int[]
student = { 1, 2, 3, 4, 5 };
int[]
student1 = new
int[10];
student1.CopyTo(student,
0);
Reverse : Reverses the sequence of the elements in the entire one-dimensional Array.
ToString() Returns a string that represents the current object
int[]
student = { 1, 2, 3 };
Array.Reverse(student);
foreach
(int
val in
student)
{
Console.WriteLine(val);
}
GetUpperBound : Gets the index of the last element of the specified dimension in the array.
int[]
student = new
int[]
{ 1, 2, 3, 4, 5 };
int
upper_bound = student.GetUpperBound(0);
Console.WriteLine(upper_bound);
//Output
:4
GetLowerBound : Returns the index of the first element of the specified dimension in the array.
int[]
student = new
int[]
{ 1, 2, 3, 4, 5 };
int
lower_bound = student.GetLowerBound(0);
Console.WriteLine(lower_bound);
//Output
:0
ForEach : For each loop through loop.
No comments:
Post a Comment