• Tidak ada hasil yang ditemukan

6 LIST

Dalam dokumen C# 1: Basic Syntax and Semantics (Halaman 104-107)

C# has a family of so-called collection classes, which are classes that can be thought of as containers for objects. These classes are discussed later, but I will introduce one of them, as you very often need. The class is called List, and indeed it is difficult in practice to write programs without having this class available.

A key concept in C# and, for that matter in any other programming language is an array, which is a data structure that may contain a number of objects of a certain type. The use of arrays is in principle quite straightforward, and there are rarely associated with the major problems to it, but there’s one problem, namely that at the time when you create an array, you have to say how many objects it must have room for. It is far from always possible, and in many cases you have to solve the problem by creating arrays that are large enough.

In other contexts, it may be necessary to count how many elements an array must have room for, and then create an array of the right size. It can lead to inefficient code, and it is precisely these difficulties that the class List can solve.

Technical is a List a class which encapsulates an ordinary array, and the class has methods for manipulating the data in that array. You should think of a List as illustrated below, where the arrow indicates the next available place:

The basic method is called Add(), and it adds an object to where the arrow points and after the object is added the arrow is moved one place right. When you create a new List the arrow is at the location to the left, corresponding to that the list is empty. As added objects, move the arrow to the right, and attempts to add an object, and there is no room, the array will automatically expands. That is exactly what is the data structure’s strength, and as a programmer you can fill objects into the list without considering whether there is room or not. The class has many other methods, so as an example you can refer to the individual objects in the list with an index and you can by an index insert elements in the middle of the list, delete elements, etc., but it is important to bear in mind that a List is a sequence, and that it can not have holes. This means in practice that if you insert an element in the middle of the list, all elements to the right of the new element is moved one place, and in the same way if you delete an element, then all elements to the right of the element that is deleted has to move one place left. In general are the class and its methods

effective, and specifically is Add() extremely effective, and as a programmer you should not think about all the technical stuff, but think of the class as a dynamic array. As an example the following method creates a List (the program ListProgram):

public static void Test1() {

List<string> list = new List<String>();

list.Add(”Gorm den Gamle”);

list.Add(”Harrald Blåtand”);

list.Add(”Svend Tveskæg”);

list.Add(”Knud den Store”);

list.Insert(3, “Harrald d. 2.”);

Console.WriteLine(list[2]);

foreach (string s in list) Console.WriteLine(s);

}

You should note the syntax of how to create the list, and how to specify that the list should be used for string objects. You must specify what it is for a kind of elements that the list should contain. Next the program add 4 elements to the list, but the next statement inserts an element in a particular space where an index indicates where you want to insert the element. The second last statement shows how to refer to an element with an index, and you do that in the same way you do for an array. Finally, the last statement, shows how to iterate through the elements in the list. You must also be aware of the property Count, which is not used in this example, but returns the number of elements in the list, and you could for example iterate through the list as follows:

for (int i = 0; i < list.Count; ++i) Console.WriteLine(list[i]);

As another example the method Test2() creates a List to integers and initialize the list with 8 integers and then determines the sum:

public static void Test2() {

List<int> list = new List<int>();

list.Add(2);

list.Add(3);

list.Add(5);

list.Add(7);

list.Add(11);

list.Add(13);

list.Add(17);

list.Add(17);

int s = 0;

for (int i = 0; i < list.Count; ++i) s += list[i];

Console.WriteLine(s);

}

The class List is defined in the namespace System.Collections.Generic, and the program must then have a using for that namespace.

The class is easy to use, and I will in the following apply it where it is needed and especially I will apply it in the final example.

Problem 11: Standard deviation

If you have a row of numbers ### , you define the standard deviation as

where my is the average value of the numbers:

Write a program where you can enter any number of decimal numbers (numbers of the type double) and store them in a List. The entry must continue until you enter 0, after which the program must print the standard deviation of the numbers.

Dalam dokumen C# 1: Basic Syntax and Semantics (Halaman 104-107)