LAB 8 Arrays (Solutions) Topics • •

Array of data types Multi-dimensional array

Array of data types An array is an indexed collection of objects, all of the same type. We can declare a C# array with the following syntax: type[] array_name;

For example: int[] myIntArray;

Actually, here we have not declared an array yet. Technically, we are declaring a variable (myIntArray) that will hold a reference to an array of integers. The square brackets ([]) tell the C# compiler that we are declaring and array, and the type specified the type of the elements it will contain. In the above example, myIntArray is an array of integers. We can instantiate an array using the new keyword. For example: myIntArray = new int[5];

This declaration creates and initializes an array of five integers, all of which are initialize to the value 0. It is important to distinguish between the array itself (which is the collection of elements) and the elements of the array. myIntArray is the array (or, more accurately, the variable that hold the reference to the array); its elements are five integers it hold. Accessing array elements We access elements of an array using the index operator ([]). Arrays are zero-based, which means that the index of the first element is always 0 – in this case, myIntArray[0]. Arrays are objects and thus have properties. One of the more useful of these is Length, which tell us how many objects are in an array. Array objects can be indexed from 0 to Length-1. That is, if there are five elements in and array, their indexes are 0, 1, 2, 3, 4. The example 1 illustrates the usage of a simple array of integer. In this example, a class named OneDimArray is a blueprint for creating a simple array of five integers. In the Main method, we first instantiate two objects, i.e., a and b, both holds the arrays of five integers. We then access the elements in the array a using a.myIntArray[i], and use the property Length to refer to the number of elements in that array. For the second array b, we call the method Power3 to initialize all of its elements, and employ the method PrintArray to write out its elements. The foreach looping statement allows us to iterate through all the elements in an array. Notice that a and b in this example are not the same array.

Computer & Programming: Arrays

1/10

Powered by MIKETM

//Example 1♦ using System; namespace MyArray { public class OneDimArray { int[] myIntArray; const int dim = 5; OneDimArray () { myIntArray = new int[dim]; } static void Power3 (OneDimArray t) { for (int i=0; i Output = 1 4 9 16 25 Computer & Programming: Arrays

7/10

Powered by MIKETM

4.3 Why does the output in 4.1 differ from the one in 4.2? เนื่องจากในขอ 4.1 ที่ method calSquare มีการ new ใหกับ array s ดังนัน้ การเปลี่ยนแปลงคาของ s ภายใน method ไมสงผลถึง ตัวแปร sqr ที่สงคามา ทําใหการทํางานผิดพลาด ในขอ 4.2 จึงแกปญหานี้โดยการใช out ซึ่งเปนการบอกวา ตัวแปรที่สงเขาให method นั้น เปน ตัวแปร output ดังนั้น เมื่อเราสงตัวแปร output sqr ให method ในรูปของ s สุดทายแลว s จะถูกสงคาคืนกลับให sqr

Computer & Programming: Arrays

8/10

Powered by MIKETM