© 2012 emc publishing, llc slide 1 chapter 8 arrays can store many of the same type of data...

15
© 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:

Upload: owen-townsend

Post on 01-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 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

© 2012 EMC Publishing, LLC

Slide 1

Chapter 8

ArraysChapter 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:

Page 2: © 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

© 2012 EMC Publishing, LLC

Slide 2

Chapter 8

Declaring and Initializing ArraysChapter 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"}

Page 3: © 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

© 2012 EMC Publishing, LLC

Slide 3

Chapter 8

Accessing Array ElementsChapter 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"

Page 4: © 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

© 2012 EMC Publishing, LLC

Slide 4

Chapter 8

Traversing an ArrayChapter 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

Page 5: © 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

© 2012 EMC Publishing, LLC

Slide 5

Chapter 8

Array As ParametersChapter 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.

Page 6: © 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

© 2012 EMC Publishing, LLC

Slide 6

Chapter 8

Arrays with Meaningful IndexesChapter 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.

Page 7: © 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

© 2012 EMC Publishing, LLC

Slide 7

Chapter 8

Linear SearchChapter 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.

Page 8: © 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

© 2012 EMC Publishing, LLC

Slide 8

Chapter 8

Dynamic ArrayChapter 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 elementsReDim 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.

Page 9: © 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

© 2012 EMC Publishing, LLC

Slide 9

Chapter 8

Two-Dimensional ArraysChapter 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:

Page 10: © 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

© 2012 EMC Publishing, LLC

Slide 10

Chapter 8

Two-Dimensional ArraysChapter 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)

Page 11: © 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

© 2012 EMC Publishing, LLC

Chapter 8

Two-Dimensional ArraysChapter 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

Slide 11

Page 12: © 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

© 2012 EMC Publishing, LLC

Slide 12

Chapter 8

StructuresChapter 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 StringDim age As IntegerDim 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

Page 13: © 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

© 2012 EMC Publishing, LLC

Slide 13

Chapter 8

Structure ArraysChapter 8

Structure Arrays

Members are accessed with a dot (.):Dim newStu As ElementaryStudentnewStu.age = 10newStu.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

ElementaryStudentstus(3).age = 10stus(3).grade = 4

Procedures can include structure parameters either ByRef or ByVal

Page 14: © 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

© 2012 EMC Publishing, LLC

Slide 14

Chapter 8

Enumerated TypesChapter 8

Enumerated Types

Defines a related set of named constants.

Enumerated type declarations are similar to:Enum Level

FreshmanSophomoreJuniorSenior

End Enum

A variable declared as an enumerated type is limited to storing the values defined in the enumeration.

Page 15: © 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

© 2012 EMC Publishing, LLC

Slide 15

Chapter 8

Arrays of ObjectsChapter 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.