Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array in C# Array in C# RIHS Arshad Khan

Similar presentations


Presentation on theme: "Array in C# Array in C# RIHS Arshad Khan"— Presentation transcript:

1 Array in C# Array in C# RIHS Arshad Khan 8 1 5 6 3 9 4 0

2 12-2 Objectives: Learn about arrays and when to use them Learn the syntax for declaring and initializing arrays and how to access array’s size and elements Learn simple array algorithms Understand two-dimensional arrays

3 12-3 What is an Array An array is a block of consecutive memory locations that hold values of the same data type. Individual locations are called array’s elements. When we say “element” we often mean the value stored in that element. 1.39 1.691.74 0.0 An array of floats

4 12-4 What is an Array (cont’d) Rather than treating each element as a separate named variable, the whole array gets one name. Specific array elements are referred to by using array’s name and the element’s number, called index or subscript. c[0]c[1]c[2]c[3] 1.39 1.691.74 0.0 c is array’s name

5 12-5 Indices (Subscripts) In C# an index is written within square brackets following data types (for example, int [ ]a). Indices start from 0; the first element of an array a is referred to as a[0] and the n-th element as a[n - 1]. An index can have any int value from 0 to array’s length - 1.

6 12-6 Indices (cont’d) We can use as an index an int variable or any expression that evaluates to an int value. For example: a [3] a [k] a [k - 2] a [ (int) (6 * Math.random()) ]

7 12-7 Indices (cont’d) In C#, an array is declared with fixed length that cannot be changed. C# editor checks the values of indices at run time and throws ArrayIndexOutOfBoundsException if an index is negative or if it is greater than the length of the array - 1.

8 12-8 Why Do We Need Arrays? The power of arrays comes from the fact that the value of a subscript can be computed and updated at run time. int sum = 0; sum += x1; sum += x2; … sum += x1000; No arrays: int n = 1000; int sum = 0, i; for (i = 0; i < n; i++) sum += x[i]; With arrays: 1000 times!

9 12-9 Declaration and Initialization When an array is created, space is allocated to hold its elements. If a list of values is not given, the elements get the default values. For example: int [ ] x= new int[10] ; char [ ] word=new char[1000]; length 10, all values set to 0 length 10000, all values set to null

10 12-10 Initialization (cont’d) An array can be declared an initialized in one statement. For example: int [ ] x = { 3,1, 7, 5, 9 };

11 12-11 Array’s Length The length of an array is determined when that array is created. The length is either given explicitly or comes from the length of the {…} initialization list. int [5] x= { 3,1, 7, 5, 9 }; int [ ] x= { 3,1, 7, 5, 9 }; // here in both cases array length is 5

12 C# Program usingarray Public static void main() { //Declares the Array int [ ] arr =new int [10] ; int i; String str = “ ” ; //Enters the values in the array arr[0]=5; arr[1]=10; 12-12

13 arr[2]=15; arr[3]=20; arr[4]=25; arr[5]=30; arr[6]=35; arr[7]=40; arr[8]=45; arr[9]=50; //Print the values of the Array 12-13

14 Message(“The Entered Array Element Are :“); for( i=0; i<=9; i++) { str += x[ i] + “\ n ”; } Message(str); } 12-14

15 THE OUTPUT IS: The Entered Array Element Are : 12-15


Download ppt "Array in C# Array in C# RIHS Arshad Khan"

Similar presentations


Ads by Google