Document 15063134

advertisement
Mata kuliah : M0874 – Programming II
Tahun
: 2010
Arrays
Session 05
Outline Materi
•Arrays
•Declaring and Allocating Arrays
•Examples Using Arrays
•Passing Arrays to Methods
•Passing Arrays by Value and Reference
•Foreach Repetition Structure
Bina Nusantara University
3
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.
Bina Nusantara University
4
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.
Bina Nusantara University
5
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
Bina Nusantara University
6
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.
Bina Nusantara University
7
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();
}
}
}
Bina Nusantara University
8
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).
Bina Nusantara University
9
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 )
Bina Nusantara University
10
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);
}
}
Bina Nusantara University
11
Passing Arrays by Value and Reference
12
References
•http://www.yoda.arachsys.com/csharp/parameters.html
•http://www.meshplex.org/wiki/C_Sharp/Arrays_II
•http://www.csharp-examples.net/sort-array/
Bina Nusantara University
13
Download