Download presentation
Presentation is loading. Please wait.
Published byOwen Townsend Modified over 9 years ago
1
© 2012 EMC Publishing, LLC Slide 1 Chapter 8 Arrays Can store many of the same type of data together. Allows a collection of related values to be stored together with a single descriptive name. A composite data type: a collection of elements Each item is called an element and each element has an index value. The friends array has 5 elements:
2
© 2012 EMC Publishing, LLC Slide 2 Chapter 8 Declaring and Initializing Arrays Declare and initialize with default values: Dim ages(4) As Integer'5 elements each 0 Declare and initialize with specific values: Dim ages() As String = {4, 10, 6, 22, 13}; Type inference can be used to determine the array type: Dim stuNames() = {"Mia", "Eli", "Eva", "Rose", "Wu"}
3
© 2012 EMC Publishing, LLC Slide 3 Chapter 8 Accessing Array Elements An array element is accessed by including its index in parentheses after the array name: Me.lblAge.Text = ages(3)'age 22 An array element is changed through assignment: friends(2) = "Sunshine"
4
© 2012 EMC Publishing, LLC Slide 4 Chapter 8 Traversing an Array The Length property returns the number of elements in an array: numElements = ages.Length'5 A For…Next loop is one way to traverse an array: For ageIndex As Integer = 0 To ages.Length – 1 ages(ageIndex) = InputBox("Enter age:") Next ageIndex
5
© 2012 EMC Publishing, LLC Slide 5 Chapter 8 Array As Parameters A procedure declaration can include array parameters: Function SumOfValues (ByRef numArray() As Integer) As Integer An array should be declared ByRef so that the actual array is passed instead of requiring a copy to be made. A procedure declaration can include an array element: Sub DisplayElement (ByVal number As Integer, ByRef lblLabel As Label) Passing just an element passes a copy of the value, preventing the element in the array from being changed.
6
© 2012 EMC Publishing, LLC Slide 6 Chapter 8 Arrays with Meaningful Indexes Use the index value of an array element for determining the storage location of a value. Simplifies storage and retrieval of data.
7
© 2012 EMC Publishing, LLC Slide 7 Chapter 8 Linear Search Simplest searching algorithm. Checks each element of an array, one after the other, until a specified value has been found or until the entire array has been checked.
8
© 2012 EMC Publishing, LLC Slide 8 Chapter 8 Dynamic Array Varies in size during run time. Used in situations where the size of an array may need to grow or shrink or when the array size is unknown at the start of the program. ReDim is used to change an array's size: Dim ages(-1) As Integer'0 elements ReDim ages(4)'5 elements ReDim can be executed over and over but all the values in the array are lost. To keep existing values use ReDim Preserve.
9
© 2012 EMC Publishing, LLC Slide 9 Chapter 8 Two-Dimensional Arrays Represents data that corresponds to a grid An element is referred to by its row and column. The TTTBoard(0, 2) element stores an X:
10
© 2012 EMC Publishing, LLC Slide 10 Chapter 8 Two-Dimensional Arrays Declaration includes the index of the last element of each dimension separated by a comma : Dim TTTBoard(2, 2) As Char ‘9 elements The Length property returns the total number of elements, 9 in the example above The number of rows is determined with a statement similar to: arrayName.GetLength(0) The number of columns is determined with a statement similar to: arrayName.GetLength(1)
11
Chapter 8 Two-Dimensional Arrays Nested For…Next loops are often used to access the elements of a two-dimensional array. A ReDim statement can be used to change the size of individual dimensions, but the number of dimensions cannot be changed once declared Two-dimensioned array parameters should be declared ByRef with the array name followed by an empty set of parameters that includes a comma indicating 2 dimensions © 2012 EMC Publishing, LLC Slide 11
12
© 2012 EMC Publishing, LLC Slide 12 Chapter 8 Structures A composite data type that groups related variables. Members can be different data types. Structure declarations are similar to: Structure ElementaryStudent Dim name As String Dim age As Integer Dim grade As Integer End Structure Structures must be declared outside of any procedure and it is done usually at the beginning of a program A structure variable can appear anywhere in the program
13
© 2012 EMC Publishing, LLC Slide 13 Chapter 8 Structure Arrays Members are accessed with a dot (.): Dim newStu As ElementaryStudent newStu.age = 10 newStu.grade = 4 An array of structures can be used to store related information for a group of elements. Members are accessed with a dot (.): Dim stus(50) As ElementaryStudent stus(3).age = 10 stus(3).grade = 4 Procedures can include structure parameters either ByRef or ByVal
14
© 2012 EMC Publishing, LLC Slide 14 Chapter 8 Enumerated Types Defines a related set of named constants. Enumerated type declarations are similar to: Enum Level Freshman Sophomore Junior Senior End Enum A variable declared as an enumerated type is limited to storing the values defined in the enumeration.
15
© 2012 EMC Publishing, LLC Slide 15 Chapter 8 Arrays of Objects Can store a reference to a set of control class objects. Declared with the appropriate control class data type. For example, Button, Label, or PictureBox. An array of control class objects can simplify code that sets the same property for multiple objects of the same type.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.