Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pemrograman VisualMinggu …7… Page 1 MINGGU Ke Tujuh Pemrograman Visual Pokok Bahasan: Arrays Tujuan Instruksional Khusus: Mahasiswa dapat menjelaskan.

Similar presentations


Presentation on theme: "Pemrograman VisualMinggu …7… Page 1 MINGGU Ke Tujuh Pemrograman Visual Pokok Bahasan: Arrays Tujuan Instruksional Khusus: Mahasiswa dapat menjelaskan."— Presentation transcript:

1

2 Pemrograman VisualMinggu …7… Page 1 MINGGU Ke Tujuh Pemrograman Visual Pokok Bahasan: Arrays Tujuan Instruksional Khusus: Mahasiswa dapat menjelaskan dan mengaplikasikan konsep Arrays pada Visual Basic 2008 Referensi: Deitel Deitel, Visual Basic 2008 (2009), How to Program, Prentice Hall. Chapter 8

3 Pemrograman VisualMinggu …7… Page 2 Agenda Declaring and Allocating Arrays Passing an Array to a Method Sorting Array With Method Sort of Class Array Searching Arrays Rectangular Arrays Jagged Arrays Changing the Size of an Array at Execution Time Passing Arrays : ByVal vs ByRef Review for MID-TERM

4 Pemrograman VisualMinggu …7… Page 3 Introduction Array adalah kumpulan yang digunakan sebagai penyimpan data sementara, digunakan untuk menyimpan sekumpulan data yang sejenis dalam sebuah variabel dan nilai yang dimasukkan dapat diakses menggunakkan indeks x(1)x(2)x(3)x(4)x(5)x(6) 10, 20, 30, …, 60 adalah nilai yang disimpan dalam struktur array 1, 2, 3 …, 10 adalah indeks dari elemen-elemen array X(1), X(2), … x(6) adalah notasi yang digunakan mengacu elemen array

5 Pemrograman VisualMinggu …7… Page 4 Declaring & Allocating Array Examples: Dim numbers As Integer() numbers = New Integer() {1,2,3,6} Examples: Dim numbers As Integer()= New Integer(11) {} Give Example Declare & Allocating Array Figure

6 Pemrograman VisualMinggu …7… Page 5 Passing an Array to a Method To pass an array argument to a method, specify the name of the array without using parentheses. Every array object “knows” its own upper bound (i.e., the value returned by the method GetUpperbound), so when you pass an array object to a method, you do not need to pass the upper bound of the array as a separate argument. For a method to receive an array through a method call, the method’s parameter list must specify that an array will be receive. In Visual basic, arrays always are passed by reference, yet it normally inappropriate to use keyword ByRef in the method definition header. Give Example passing an Array to a Method Figure 8.11

7 Pemrograman VisualMinggu …7… Page 6 Sorting Array With Method Sort of Class Array Sorting data (i.e., arranging the data in some particular order, such as ascending or descending order) is one of the most popular computing applications. Class Array provides methods for creating, modifying, sorting and searching arrays. Shared method Sort of class Array takes an array as its argument and sorts the elements in the array in ascending order. To sort an array in descending order, first call method Sort of class array to sort the array, then call method Reverse of class Array to reverse the order of the elements in the array. Give Example Sorting Array with Method Sort of Class Array Figure 8.15

8 Pemrograman VisualMinggu …7… Page 7 Searching Arrays Often it is necessary to determine whether an array contains a value that matches a certain key value. The process of locating a particular element value in an array is called searching. The linear search method works well for small or unsorted arrays. However, it is inefficient for large unsorted arrays. If the array is sorted, the high-speed binary search technique can be used. The Array class provides method BinarySearch, which searches a sorted array for a value using binary search. Give Example Searching Array Figure 8.16 – 8.18

9 Pemrograman VisualMinggu …7… Page 8 Rectangular Arrays One dimensional arrays contain one row of values. Multidimensional arrays require two or more indices to identify particular elements. There are two types of two dimensional arrays, rectangular and jagged. Rectangular arrays with two indices often represent tables of values consisting of information arranged in rows and columns. Each row is the same size, and each column is the same size. Give Example Rectangular Array Figure 8.20

10 Pemrograman VisualMinggu …7… Page 9 Jagged Arrays Jagged arrays are maintained as arrays of arrays. Unlike rectangular arrays, rows in jagged arrays can be of different lengths. In a jagged array, the second dimension is actually an index into the one-dimensional array that represents the current row. Arrays of two or more dimensions can be traversed using a nested For…Next statement with a separate For…Next statement for each dimension Give Example Jagged Array Figure 8.24

11 Pemrograman VisualMinggu …7… Page 10 Changing the Size of an Array at Execution Time The ReDim statement enables you to dynamically change the array size by creating a new array and assigning its reference to the specified variable. The ReDim statement contains keyword ReDim, followed by the name of the array to be resized and the new upper bound in parentheses. The ReDim Statement changes the size of the array and reinitializes the value of the elements to the default value of the type of the array element. To save the original data stored in an array, follow the ReDim keyword with the optional preserve keyword. If the new array is smaller than the original array, the existing elements that are outside the bounds of the new array are discarded. If the new array is larger than the original array, all the existing elements are preserved in the now larger array, and the extra elements are initialized to the default value of the type of the array element. Give Example Changing the size of an Array at Execution Time Figure 8.25

12 Pemrograman VisualMinggu …7… Page 11 Passing Arrays : ByVal vs ByRef If an argument passed using keyword ByVal is of a reference type, the value being copied is a reference to the original object in memory, any changes you make using that reference will be applied to the original object. Visual basic also allows methods to pass references with keyword byRef When a reference type object like an array is passed with ByRef, the called method gains control over the original reference in the caller, allowing the called method to replace the original reference in the caller with a reference to a different object or nothing. Such behavior can lead to unpredictable effects, which can be disastrous in business-critical and mission-critical applications Visual basic provides operator IS for comparing references to determine whether they are referencing the same object. Give Example Passing Arrays: ByVal vs ByRef Figure 8. 26

13 Pemrograman VisualMinggu …7… Page 12 Questions & Answers

14 Pemrograman VisualMinggu …7… Page 13 Thank You


Download ppt "Pemrograman VisualMinggu …7… Page 1 MINGGU Ke Tujuh Pemrograman Visual Pokok Bahasan: Arrays Tujuan Instruksional Khusus: Mahasiswa dapat menjelaskan."

Similar presentations


Ads by Google