tailieunhanh - Beginning Visual Basic 2005 phần 3

Bạn cũng có thể gọi phương thức ngược trên lớp Array để đảo ngược thứ tự của mảng và sau đó sử dụng của bạn Đối với mỗi. . . Tiếp theo vòng lặp, như thể hiện trong kế tiếp thử It Out Đảo ngược Một mảng 1. Thêm một điều khiển nút trong thiết kế mẫu và thiết lập thuộc tính Name của nó | Chapter 5 For intIndex As Integer O To 0 Step -1 Add the array item to the list strFriends 0 Next You can also call the Reverse method on the Array class to reverse the order of the array and then use your For Each . . . Next loop as shown in the next Try It Out. Try It Out Reversing An Array 1. Add another Button control in the Form Designer and set its Name property to btnReversingAnArray and its Text property to Reversing an Array. 2. Double-click the button and add the following highlighted code to its Click event handler Private Sub btnReversingAnArray_Click ByVal sender As _ ByVal e As Handles Reverse the order - elements will be in descending order strFriends List your friends AddItemsToList strFriends End Sub 3. Run the project and click the button. You ll see the friends listed in reverse order as shown in Figure 5-5. Figure 5-5 How It Works The Reverse method reverses the order of elements in a one-dimensional array which is what you are working with here. By passing the strFriends array to the Reverse method you are asking the Reverse method to resequence the array from bottom to top Reverse the order - elements will be in descending order strFriends 134 Working with Data Structures Once the items in your array have been reversed you simply call the AddltemsToList procedure to have the items listed List your friends AddltemsToList strFriends If you want to list your array in descending sorted order you would call the Sort method on the Array class to have the items sorted in ascending order and then call the Reverse method to have the sorted array reversed putting it into descending order. Initializing Arrays with Values It is possible to create an array in Visual Basic 2005 and populate it in one line of code rather than having to write multiple lines of code to declare and populate the array as shown here Declare an array .

TỪ KHÓA LIÊN QUAN