Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 47 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
47
Dung lượng
1,02 MB
Nội dung
Visual basic .Net Arrays Programming with Microsoft Visual Basic .NET, Second Edition Programming with Microsoft Visual Basic .NET, Second Edition 2 Using a One-Dimensional Array Lesson A Objectives • Declare and initialize a one-dimensional array • Store data in a one-dimensional array • Display the contents of a one-dimensional array • Code a loop using the For Each…Next statement • Access an element in a one-dimensional array Programming with Microsoft Visual Basic .NET, Second Edition 3 Using a One-Dimensional Array Lesson A Objectives (continued) • Search a one-dimensional array • Compute the average of a one-dimensional array’s contents • Find the highest entry in a one-dimensional array • Update the contents of a one-dimensional array • Sort a one-dimensional array Programming with Microsoft Visual Basic .NET, Second Edition 4 Arrays • A simple or scalar variable is one that is unrelated to any other variable in memory • An array is a group of variables that have the same name and data type and are related in some way Programming with Microsoft Visual Basic .NET, Second Edition 5 Arrays (continued) • The most commonly used arrays are one- dimensional and two-dimensional • Programmers use arrays to store related data in the internal memory of the computer Programming with Microsoft Visual Basic .NET, Second Edition 6 One-Dimensional Arrays • A one-dimensional array is simply a row (or column) of variables • Each element in an array is identified by a subscript, which Visual Basic .NET assigns to the variable when the array is created Programming with Microsoft Visual Basic .NET, Second Edition 7 One-Dimensional Arrays (continued) • You refer to each variable in an array by its name and the variable’s subscript Figure 10-3: Names of the variables in a one-dimensional array named states Programming with Microsoft Visual Basic .NET, Second Edition 8 One-Dimensional Arrays (continued) • Declaring a one-dimensional array – Version 1 {Dim | Private} arrayname(highestSubscript) As datatype – Version 2 {Dim | Private} arrayname() As datatype = {initialValues} Programming with Microsoft Visual Basic .NET, Second Edition 9 One-Dimensional Arrays (continued) • Examples of declaring an array – Dim cities(3) As String – Private states() As String = {“Hawaii”, “Alaska”, “Maine”} Programming with Microsoft Visual Basic .NET, Second Edition 10 Storing Data in a One-Dimensional Array • In most cases, you use an assignment statement to enter data into an existing array • Syntax: arrayname(subscript) = value • Examples – cities(0) = “Madrid” – cities(1) = “Paris” – cities(2) = “Rome” [...]... Programming with Microsoft Visual Basic NET, Second Edition 24 More on One-Dimensional Arrays Lesson B Objectives • Create and manipulate parallel one-dimensional arrays • Create a structure • Declare a structure variable • Create and manipulate a one-dimensional array of structures Programming with Microsoft Visual Basic NET, Second Edition 25 Parallel One-Dimensional Arrays • Arrays that are related... and a number, you can use two one-dimensional arrays – A String array to store the product IDs – An Integer array to store the prices Programming with Microsoft Visual Basic NET, Second Edition 26 Parallel One-Dimensional Arrays (continued) Figure 10-19: Illustration of a price list stored in two onedimensional arrays Programming with Microsoft Visual Basic NET, Second Edition 27 Structures • Structure... • Search a two-dimensional array Programming with Microsoft Visual Basic NET, Second Edition 33 Two-Dimensional Arrays • A two-dimensional array resembles a table in that the variables are in rows and columns Figure 10-32: Illustration of a two-dimensional array Programming with Microsoft Visual Basic NET, Second Edition 34 Two-Dimensional Arrays (continued) • Each variable (element) in a two-dimensional... a two-dimensional array by the array’s name and the row and column subscripts Programming with Microsoft Visual Basic NET, Second Edition 35 Two-Dimensional Arrays (continued) Figure 10-34: Syntax versions and examples of declaring a twodimensional array Programming with Microsoft Visual Basic NET, Second Edition 36 ... Programming with Microsoft Visual Basic NET, Second Edition 22 Sorting the Data Stored in a OneDimensional Array (continued) • To sort a one-dimensional array in descending order: – Use Array.Sort to sort the array in ascending order – Use Array.Reverse to reverse the array elements • Syntax of the Array.Reverse method: Array.Reverse(arrayname) Programming with Microsoft Visual Basic NET, Second Edition 23... Microsoft Visual Basic NET, Second Edition 12 Displaying the Contents of a OneDimensional Array • uiDisplayButton’s Click event procedure – Demonstrates how you can display the contents of an array in a label control – Uses the For…Next statement to display each array element – You also could use the Do…Loop statement or the For Each…Next statement Programming with Microsoft Visual Basic NET, Second... a OneDimensional Array (continued) Figure 10-6: uiDisplayButton’s Click event procedure Programming with Microsoft Visual Basic NET, Second Edition 14 The For Each…Next Statement Figure 10-8: Syntax and an example of the For Each…Next statement Programming with Microsoft Visual Basic NET, Second Edition 15 Using the Subscript to Access an Element in a One-Dimensional Array • XYZ Corporation pays its... Structure statement: can be used to create your own data types in Visual Basic NET • Data types created using the Structure statement are referred to as user-defined data types or structures • Members included in the structure can be variables, constants, or procedures • In most cases, members are variables Programming with Microsoft Visual Basic NET, Second Edition 28 Structures (continued) Figure 10-21:... an example of the Structure statement Programming with Microsoft Visual Basic NET, Second Edition 29 Using a Structure to Declare a Variable • Variables declared using a structure are often referred to as structure variables Figure 10-22: Syntax and an example of declaring a structure variable Programming with Microsoft Visual Basic NET, Second Edition 30 Using a Structure to Declare a Variable (continued)... data in a member variable Programming with Microsoft Visual Basic NET, Second Edition 31 Creating an Array of Structure Variables • Assigning initial values to an array is referred to as populating the array • Refer to a member variable in an array element using the syntax: arrayname(subscript).memberVariableName Programming with Microsoft Visual Basic NET, Second Edition 32 Using a Two-Dimensional Array . Visual basic .Net Arrays Programming with Microsoft Visual Basic .NET, Second Edition Programming with Microsoft Visual Basic .NET, Second Edition 2 Using a One-Dimensional. by a subscript, which Visual Basic .NET assigns to the variable when the array is created Programming with Microsoft Visual Basic .NET, Second Edition 7 One-Dimensional Arrays (continued) • You. way Programming with Microsoft Visual Basic .NET, Second Edition 5 Arrays (continued) • The most commonly used arrays are one- dimensional and two-dimensional • Programmers use arrays to store related