Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays Session 05 Mata kuliah: M0874 – Programming II Tahun: 2010.

Similar presentations


Presentation on theme: "Arrays Session 05 Mata kuliah: M0874 – Programming II Tahun: 2010."— Presentation transcript:

1

2 Arrays Session 05 Mata kuliah: M0874 – Programming II Tahun: 2010

3 Bina Nusantara University 3 Outline Materi Arrays Declaring and Allocating Arrays Examples Using Arrays Passing Arrays to Methods Passing Arrays by Value and Reference Foreach Repetition Structure

4 Bina Nusantara University 4 Arrays An array is a group of contiguous memory locations that al have the same name and type. To refer to a particular location or element in the array, we specify the name of the array and the position number of the element to which we refer. A program can refer to any element of an array by giving the name of the array followed by the position number of the element in square brackets ([]). The first element in every array is the zeroth element.

5 Bina Nusantara University 5 Declaring and Allocating Arrays Arrays occupy space in memory. String strEmotion = "happy"; String[] strEmotions = {"happy","sad","elated","afraid","angry","peaceful"}; int[] arr = new int[10]; double numbers = new double[5]; string[] names = new string[2]; By using the brackets [], the compiler knows that the variable strEmotions will hold more than one value.

6 Bina Nusantara University 6 Declaring and Allocating Arrays String [] strEmotions = {"happy","sad","elated","afraid","angry","peaceful"}; strEmotions[0] = happy Value in the 0 position of the array strEmotions[1] = sad Value in the 1 position of the array strEmotions[2] = elated Value in the 2 position of the array strEmotions[3] = afraid Value in the 3 position of the array strEmotions[4] = angry Value in the 4 position of the array strEmotions[5] = peaceful Value in the 5 position of the array

7 Bina Nusantara University 7 Examples Using Arrays If you do not initialize an array at the time of declaration, the array members are automatically initialized to the default initial value for the array type. Also, if you declare the array as a field of a type, it will be set to the default value null when you instantiate the type.

8 Bina Nusantara University 8 using System; namespace array_example1 { class Program { static void Main(string[] args) { int[] arr = new int[10]; for (int x = 0; x < 10; x++) { Console.WriteLine("Enter array element : {0}", x + 1); arr[x] = Int32.Parse(Console.ReadLine()); } foreach (int i in arr) { Console.WriteLine(i); } Console.ReadLine(); }

9 Bina Nusantara University 9 Passing Arrays to Methods To pass an array argument to a method, specify the name of the array without using brackets. For example, if array hourlyTemperatures declared as int[] hourlyTemperatures = new int [ 24 ]; The method call ModifyArray( hourlyTemperatures ); Every array object knows its own size (via the length instance variable).

10 Bina Nusantara University 10 Passing Arrays to Methods To receive an array through a method call, the method’s parameter list must specify that an array will be received. public void ModifyArray( int[] b )

11 Bina Nusantara University 11 Passing Arrays public class Example9 { static void ShowNumbers (params int[] numbers) { foreach (int x in numbers) { Console.Write (x+" "); } Console.WriteLine(); } public static void Main (string[] args) { int[] x = {1, 2, 3}; ShowNumbers (x); ShowNumbers (4, 5); }

12 12 Passing Arrays by Value and Reference

13 Bina Nusantara University 13 References


Download ppt "Arrays Session 05 Mata kuliah: M0874 – Programming II Tahun: 2010."

Similar presentations


Ads by Google