CHP 05 Arrays
-
Upload
ashish8912003 -
Category
Documents
-
view
220 -
download
0
Transcript of CHP 05 Arrays
-
8/8/2019 CHP 05 Arrays
1/16
1
C#
-
8/8/2019 CHP 05 Arrays
2/16
2
ARRAYS
In C#, arrays is a data structure.
An array is a collection of data having same datatype, storage class and name; and resides inconsecutive memory locations
The individual array elements are distinguishedby an index value, which starts with zero.
This means, the highest index number of anarray is always the no of elements minus one.
Array type can be of any type.
-
8/8/2019 CHP 05 Arrays
3/16
3
ARRAYS In C#
In C#, Arrays are a collection of objects or datatypes, which are stored in memory cells.
All the elements of an array must be of the sametype.
In C#, array element type is any one of theprimitive data type or any reference type.
In C# arrays are treated as objects havingSystem.Array as their base class
C# supports two types of arrays. Single Dimensional Arrays
Multi dimensional Arrays
-
8/8/2019 CHP 05 Arrays
4/16
4
The general form for creating an array in C# is:Syntax: element-type[ ] name;
name = new element-type[n];
Here, the first statement declares an array.
The second statement allocates memory spacefor n elements.
C# allows you to have a declaration and allocation
in a single statement like below:Syntax:
element-type[ ] name = new element-type[n];
Single Dimension Array
-
8/8/2019 CHP 05 Arrays
5/16
5
Single Dimension Array
Examples:
float[ ] amt = new float[6];
char[ ] cx = new char[9];
string[ ] s;
s = new string[8];
box [] bx = new box[7];
Once memory is allocated, all the elements ofarrays are initialized to their default values.
Single dimension array can be initialized whiledeclaring or separately after declaring.
We can find out the number of elements in anarray using Array.Length property
-
8/8/2019 CHP 05 Arrays
6/16
6
Initialization
Syntax:
type [] x = new type[n];
x [0]= e1;
x [1]= e2;
.
.
x [n-1]= en;
EX:
int [] x = new int[2];
x[0]= 10;
x[1]=11;
Single Dimension Array
-
8/8/2019 CHP 05 Arrays
7/16
7
Initialization
Syntax:
type[] x = {e1, e2, e3, .eN};
Here the size of the array is determined by the number ofelements listed in { }.
The above initialization can be done using new operator
type[] x = new type[ ]{e1, e2, e3, .eN};
Alternative syntax can also be used:
type[] x = new type[ N]{e1, e2, e3, .eN-1};
Single Dimension Array
-
8/8/2019 CHP 05 Arrays
8/16
8
Iterating through array
int [ ] x = {1,2,3,4,5,6};
for (int i = 0; i
-
8/8/2019 CHP 05 Arrays
9/16
9
MULTI DIMENSIONAL ARRAY
Multi Dimensional arrays can be either: Rectangle Array
Jagged Array
Multidimensional arrays are nothing but, array of
arrays.
In rectangular array, all the arrays at one levelmust have the same dimensions
In Jagged array, all the arrays at one level neednot have same dimensions
-
8/8/2019 CHP 05 Arrays
10/16
10
Two dimensional array:
Declaration:
type [,] xy = new type [m,n];
type [,] xy;
xy = new type [m,n];Initialization
Individual elements
type [,]xy = {{e1,e2}, {e3,e4}, . {eN-1,eN}};
type [,]xy = new int[n, m]{{e1,e2}, {e3,e4}, . {eN-1,eN}};
Rectangle Array
-
8/8/2019 CHP 05 Arrays
11/16
11
Rectangle Array
Rectangle Array
Initializing example:
int [,] xy = new int[2,2]; // length 4, rank 2
xy[0,0] = 34; xy[0,1] = 24;
xy[1,0] = 44; xy[1,1] = 45;
int [,] xy = {{24,34},{44,45}};
int [,] xy = new int [2,2] {{24,34},{44,45}};
Length property of returns the total number ofelements in the array.
-
8/8/2019 CHP 05 Arrays
12/16
12
Three dimensional array:
int [, ,] xyz = new int [n1, n2, n3];
Four dimensional array:
int [, , ,] xyz = new int [n1, n2, n3, n4];
The number of commas in the declaration will beone less than the dimensions (rank) of the array.
We can find out the rank of an array usingArray.Rank property
We can use GetLength method to get the size ofeach dimension.
Arrayname.GetLength(dimension-postion); postion startsfrom 0
Rectangle Array
-
8/8/2019 CHP 05 Arrays
13/16
13
Iterating through array
int [ ,] x = new int [2,3]{{1,2},{3,4},{5,6}};
for (int i = 0; i
-
8/8/2019 CHP 05 Arrays
14/16
14
Jagged arrays are used when we needmultidimensional array, but where the size ofinternal arrays is not same.
Declaration:
type [] [] x; Declares a single dimensional arrayof single dimensional arrays.
type [] [ , ] x; Declares a single dimensional arrayof two dimensional arrays.
type [] [ , ] [] x; Declares a single dimensional arrayof two dimensional arrays ofsingle dimensional arrays.
Jagged Array
-
8/8/2019 CHP 05 Arrays
15/16
15
Example:int [] [] x = new int [4] []; // length is 4, rank is 1
x [0] = new int [3]{1,2,3};
x [1] = new int [2]{1,2};
x [2] = new int [5]{1,2,3,4,5};x [3] = new int [7]{1,2,3,4,5,6,7};
Jagged arrays can not be initialized whiledeclaring them like other arrays.
Rank property of a jagged array return the rank ofthe first array.
int [,] [] [,,] x ; //Rank of this array is 2
Jagged Array
-
8/8/2019 CHP 05 Arrays
16/16
16
Iterating through array
int [] [] x = new int [3] [];
x [0] = new int [3]{1,2,3};
x [1] = new int [2]{1,2};
x [2] = new int [5]{1,2,3,4,5};
foreach (int[] i in x)
foreach(int j in i)Console.WriteLine(j);
Jagged Array