Download presentation
Presentation is loading. Please wait.
1
Arrays
2
A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays are has System.Array type. So that they could use all functions of System.Array class.
3
Arrays As a simple example, think of days within a week. This a one dimensional array whose first element is monday and last element is sunday. Another example is, the days within a month forms 2D (two dimensional) array. At horizontal there are the days of week and at horizontal dimention there are weeks. We may think days of a year as a 3D array. 1st dimension is days of week, 2nd dimension is week and 3rd dimension is months.
4
Arrays One dimensional array is defined with a variable name, type and its length given between square brackets. Exaple, int[] days = new int[ 7 ]; statement defines a one dimensional array named days with seven elements. Since this is a integer array, elements of it will take initial value of zero.
5
Use of array After defining array, we access its elements with array name and index number within square brackets, e.g. name_of_array[index] In C# language first element in an array is at index zero (0). Forexample, we can access at least 0 and at most 6th index in days array The numbers within square brackets are called index numbers.
6
Index Numbers day[0]day[1]day[2]day[3]day[4]day[5]day[6] 26641254-10 int[] day = new int[7]; day[5] = 1; if( day[5] == 4 ) break; day[5] = day[6] - 1; day[5] = 1; if( day[5] == 4 ) break; day[5] = day[6] - 1; Examples:
7
Example Variable i will take values from 0 to 6 within for loop
8
Initialization of arrays As in the case of variables, we could also set initial values to the arrays in the definition phase. static void Main(string[] args) { int[] day = { 0,2,4,6,8,10,11 };..........} static void Main(string[] args) { int[] day = { 0,2,4,6,8,10,11 };..........}
9
Initialization of arrays static void Main(string[] args) { int[] day = { 0,2,4,6,8,10,11 }; } [] Compiler counts the integer values and decides the value in the [] brackets is 7 and compiles the program as int[] gun = new int[7] [] Compiler counts the integer values and decides the value in the [] brackets is 7 and compiles the program as int[] gun = new int[7]
10
Initialization of arrays If we are not assigning initial values to an array, then elements of array will take initial values depending on the type of the array. For example numerical types will be zero and string and other reference types will be “null”. Warning: Remember that a variable of type string having value of null null doesn’t mean that it is an empty string. null means that there is no memory allocated for that variable and its value can not be used in expressions yet.
11
Example Define an array of double type that has 10 elements, then assign values between 1.0 and 10.0. Finally swap elements at 0 and 9 with each other. static void Main(string[] args) { double[] arr = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}; 9.0, 10.0}; double tmp; tmp = arr[9]; arr[9] = arr[0]; arr[0] = tmp; }
12
Exercise Write a program that generates 100 random numbers and puts them to an array. Program should write smallest and biggest numbers in the array to the output. Also program should calculate the mean value of the array and print it to the output.
13
Exercise - Solution The program below finds maximum and minimum values. Add calculation of mean value feature to the program.
14
Array.Length() Used in finding the length of the array. Called right after the “.” mark after the array name. int[] myArray = new int[5]; int len = myArray.Length(); Console.WriteLine(len); Output : 5
15
foreach Loop This is a loop that iterates for each element in a loop. It is forward only. It look like for loop
16
foreach foreach and for loops can be converted to each other.
17
Multidimensional Arrays These are arrays with more than one dimension. As an example we can use multidimensional arrays for modeling a chess board.
18
Standard multidimensional arrays Creation of multidimensional arrays are similar to one dimensional ones. The difference is there is a comma between the square brackets. int [,] numbers; string [,,] lines; As an example, a string type two by three multidimensional array can be created with string[,] array = new string[2,3];
19
Standard multidimensional arrays 123 9205 240 670 4211 array All dimension lengths are equal in standard arrays. Therefore they look like matrices.
20
Standard multidimensional arrays Lets create a 2D multidimensional array and assign values to its elements in a loop.
21
Array operations Searching and sorting
22
Array Operations Some of the most used array functions in programming; IndexOf() LastIndexOf() Sort() Reverse() They are defined within Array class.
23
IndexOf() - LastIndexOf() IndexOf() : Starts searching from first element and returns the index number of element found. LastIndexOf() : Starts searching from last element and returns the index number of element found. If the element that we are looking for is not in the list, that these function return -1.
24
Array.IndexOf(), Array.LastIndexOf() Result : 1 Result : 4
25
Array.Sort() Sorts the elements of the array by ascending order. Result : Ali Mehmet Pınar Zeynep
26
Array.Reverse() Reverses the order of the elements of the array. Result : 45 32 9 5 3 2 1 Not : Array.Sort and Array.Reverse does not have to be used together.
27
Problem If we don’t know the size of the array in advance? What if array expands dynamically?
28
Collections Used in situations of which the length of the array is not known in advance. Or the length of the array grows or shrinks dynamically. They are one dimensional.
29
ArrayList It is one of the basic collection types. Resides in System.Collections namespace. It has initial length of 16 and for each expansion, its length is multiplied by 2. Stores data as type of object.
30
ArrayList Creating a new ArrayList: ArrayList list = new ArrayList(); Example
31
ArrayList Add Capacity Clear Count IndexOf Insert RemoveAt Reverse Sort TrimToSize Important functions and properties
32
Exercise Define an ArrayList to hold the names of staff. Add the names; Ahmet, Mehmet, Pınar, Yeşim, Utku, Sinan, Fatma, Ayşe to the list. Print the list and its size to output. Delete the elements which are stating with letter ‘A’ from the list Sort the list with reverse order. Print the list and its size to output.
33
Exercise Solution
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.