Arrays. A group of data with same type stored under one variable. It is assumed that elements in...

Preview:

Citation preview

Arrays

Arrays 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.

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.

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.

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.

Index Numbers

day[0] day[1] day[2] day[3] day[4] day[5] day[6]

2 6 6 4 12 54 -10

int[] day = new int[7];int[] day = new int[7];

day[5] = 1;day[5] = 1;if( day[5] == 4 ) break;if( day[5] == 4 ) break;day[5] = day[6] - 1;day[5] = day[6] - 1;

day[5] = 1;day[5] = 1;if( day[5] == 4 ) break;if( day[5] == 4 ) break;day[5] = day[6] - 1;day[5] = day[6] - 1;

Examples:Examples:

Example

Variable i will Variable i will take values from take values from 0 to 6 within for 0 to 6 within for

looploop

Variable i will Variable i will take values from take values from 0 to 6 within for 0 to 6 within for

looploop

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)static void Main(string[] args){{

int[] day = int[] day = { 0,2,4,6,8,10,11 }{ 0,2,4,6,8,10,11 };;....................

}}

static void Main(string[] args)static void Main(string[] args){{

int[] day = int[] day = { 0,2,4,6,8,10,11 }{ 0,2,4,6,8,10,11 };;....................

}}

Initialization of arrays

static void Main(string[] args)static void Main(string[] args){{

int[] day = int[] day = { 0,2,4,6,8,10,11 }{ 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]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]int[] gun = new int[7]

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.

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)static void Main(string[] args){{

double[] arr = {1.0, 2.0, 3.0, 4.0,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;double tmp;tmp = arr[9];tmp = arr[9];arr[9] = arr[0];arr[9] = arr[0];arr[0] = tmp;arr[0] = tmp;

}}

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.

Exercise - SolutionThe program below finds maximum and minimum values. Add calculation of mean value feature to the program.

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

foreach Loop

This is a loop that iterates for each element in a loop. It is forward only.

It look like for loop

foreach

foreach and for loops can be converted to each other.

Multidimensional Arrays These are arrays with more than one

dimension.

As an example we can use multidimensional arrays for modeling a chess board.

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];

Standard multidimensional arrays

1 2 3

9 20 5

2 4 0

6 7 0

4 2 11

array

All dimension lengths are equal in standard arrays. Therefore they look like matrices.

Standard multidimensional arrays

Lets create a 2D multidimensional array and assign values to its elements in a loop.

Array operations

Searching and sorting

Array Operations

Some of the most used array functions in programming; IndexOf()LastIndexOf()Sort()Reverse()

They are defined within Array class.

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.

Array.IndexOf(), Array.LastIndexOf()

Result : 1

Result : 4

Array.Sort()

Sorts the elements of the array by ascending order.

Result : AliMehmetPınarZeynep

Array.Reverse()Reverses the order of the elements of the array.

Result : 4532 9 5 3 2 1

Not : Array.Sort and Array.Reverse does not have to be used together.

Problem

If we don’t know the size of the array in advance?

What if array expands dynamically?

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.

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.

ArrayList

Creating a new ArrayList:ArrayList list = new

ArrayList();Example

ArrayList

AddCapacityClearCountIndexOfInsert

RemoveAtReverseSortTrimToSize

Important functions and properties

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.

ExerciseSolution

Recommended