26
1 Microsoft Visual Basic 2010 Arrays

1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to: Declare

Embed Size (px)

Citation preview

Page 1: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

1

Microsoft Visual Basic 2010Arrays

Page 2: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

2

Using a One-Dimensional ArrayLesson A Objectives

After completing this lesson, you will be able to:

Declare and initialize a one-dimensional array

Assign data to a one-dimensional array

Display the contents of a one-dimensional array

Access an element in a one-dimensional array

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

Page 3: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

3

Arrays

A simple variable, also called a 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

Although arrays in Visual Basic can have as many as 60 dimensions, 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

Page 4: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

4

One-Dimensional Arrays

A one-dimensional array is simply a row (or column) of variables

A two-dimensional array resembles a table in that it has rows and columns

Each element in an array is identified by a subscript, which Visual Basic assigns to the variable when the array is created

You refer to an array element by the array’s name followed by the element’s subscript

Page 5: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

5

One-Dimensional Array

Alaska Montana South Carolina Tennessee Texas

Alaska

Montana

South Carolina

Tennessee

Texas

Page 6: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

6

Declaring an Array

Version 1

accessibility arrayname(highestSubscript) As datatype

Version 2

accessibility arrayname() As datatype = {initialValues}

These statements create and initialize the array variables in memory

accessibility is Dim, Public, or Private

Page 7: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

7

Declaring an array

Dim strCitys(3) As String

Private intNumbers(5) As Integer

Private udtItems(4) As ItemStruc

Private strStates() As String = {“Hawaii”, “Alaska”, “Maine”}

Dim intScores() As Integer = {75, 9, 23, 6}

Page 8: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

8

Storing Data in a One-Dimensional Array

You can use a variety of ways to enter data into an array

strMonthArray(0) = “Jan”

strMonthArray(1) = “Feb”

strMonthArray(2) = “Mar”

strMonthArray(3) = “Apr”

strMonthArray(4) = “May”

strMonthArray(5) = “June”

Page 9: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

9

Assigning Values to Array Elements

For intNum = 1 to 6

intSquareArray(intNum - 1) = intNum * intNum

Next intNum

For intNum = 0 to 10

sngNum(intNum) = Val(InputBox(“Enter number”))

Next intNum

udtItems(0).sngPrice = udtItems(0).sngPrice * 1.1

Page 10: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

10

One-Dimensional Array

Dim intX As Integer

Do While (intX <= 5 AndAlso Not EOF(1))

FileGet(1, udtItems(intX), intX + 1)

intX += 1

Loop

Page 11: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

11

Display the Contents of a One-Dimensional Array

Dim strMonths() As String = {“JAN”, “FEB”, “MAR”,

“APR”, “MAY”, “JUN”, “JUL”, “AUG”, “SEP”,

“OCT”, “NOV”, “DEC”}

Dim intX As Integer

For intX = 0 To strMonths.Length - 1

Me.MonthListBox.Items.Add(strMonths(intX))

Next intX

Page 12: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

12

Searching a One-Dimensional Array

Dim intSales() As Integer = {45000, 35000, 25000, 60000, 23000}

Dim intX As Integer ‘keeps track of subscriptsDim intCount As Integer ‘counter variableDim intSearchFor As Integer ‘number to search forintSearchFor = Val(InputBox(“Enter sales to search for:”, _

“Sales”))For intX = 0 To 4

If intSales(intX) > intSearchFor ThenintCount += 1

End IfNext intXMessageBox.Show(“Count: ” & intCount, “Sales”, _

MessageBoxButtons.OK, MessageBoxIcon.Information)

Page 13: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

13

Calculating the Average Amount Stored in a

One-Dimensional Numeric ArrayDeclare variables

Repeat for each score

add array score to intTotal variable

End repeat for intNum

Calculate the average by dividing intTotal by number of scores

Display the average

Page 14: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

14

Calculating the Average Amount Stored in

a One-Dimensional Numeric ArrayDim intScores() As Integer = {98, 100, 56, 74, 35}

Dim intX As Integer ‘keeps track of subscripts

Dim intTotal As Integer ‘accumulator variable

Dim sngAvg As Single ‘average score

For intX = 0 To intScores.Length - 1

intTotal += intScores(intX)

Next intX

sngAvg = intTotal / intScores.Length

MessageBox.Show(“Average: ” & sngAvg, “Average”, _

MessageBoxButtons.OK, MessageBoxIcon.Information)

Page 15: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

15

Determining the Highest ValueStored in a One-Dimensional Array

Declare variablesAssign first array value (zero) to intHigh variableInitialize the intX variable to 1 (second subscript) Repeat while intX is less than the number of

elements in the array If current array value > intHigh value then

assign current array value to intHighEnd If

End repeat for intNumDisplay the highest value(stored in intHigh)

Page 16: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

16

Updating the Values Stored in a One-Dimensional Array

Declare variables

Prompt user for the value to add

Repeat for each price in the array

add value to current array value

display the contents of the current array element

End repeat

Page 17: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

17

Sorting the Data Stored in a One-Dimensional Array

declare an Integer array named intNumbersdeclare an Integer variable named intXopen the random access file named nums.datarepeat while intX is less than or equal to 5 and it is not the end of the

nums.data fileread a number from the file and store it in the current array elementdisplay the contents of the current array element in a message boxadd 1 to the intX variable

end repeatclose the nums.data filesort the intNumbers array in ascending order using the Array.Sort methodrepeat for each element in the intNumbers array

display the contents of the current array element in a message boxend repeat

Page 18: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

18

Summary of Array Methods

Clear Sets a range of elements in the Array to zero or to a null reference (Nothing in Visual Basic).

Copy Copies a section of one Array to another Array

IndexOf Returns the index of the first occurrence of a value in a one-dimensional Array or in a portion of the Array

LastIndexOf Returns the index of the last occurrence of a value in a one-dimensional Array or in a portion of the Array

Reverse Reverses the order of the elements in a one-dimensional Array or in a portion of the Array

Sort Sorts the elements in one-dimensional Array objects

Page 19: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

19

Parallel Arrays

Arrays that are related by an element’s position (subscript)

Searching one array gives you the subscript for the other array

Dim strId() As String = {"BX35”, “CR20", “FE15”, “KW10”, “MM67”}

Dim intPrice() As Integer = {13, 10, 12, 24, 4}Dim intX As Integer, strSearchFor As StringstrSearchFor = UCase(IdTextBox.Text)‘search the arrayDo While intX < strId.Length AndAlso

strSearchFor <> strId(intX)intX += 1

Loop

Product ID Price

BX35 13

CR20 10

KW10 24

MM67 4

Page 20: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

20

An Array of Structures

Declare an ItemStruc array named udtPriceListDeclare variables intX and strSearchForAssign IDs and prices to the udtPriceList arrayAssign the product ID entered in the IdTextBox control, converted to

uppercase, to the strSearchFor variableRepeat while intX is less than the list length and the value of

strSearchFor variable is not equal to the value stored in the current array element’s strId fieldIncrement intX

End repeatIf the intX variable contains a number that is less than the lists length

display the appropriate price from the intPrice field in the arrayElse

display the message “Product ID is not valid” End if

Page 21: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

21

An Array of StructsStructure ItemStruc

Public strId As String

Public intPrice As Integer

End Structure

Dim udtItem(4) As ItemStruc

‘populate the array

udtItem(0).strId = “BX35”: udtItem(0).intPrice = 13

‘search the array

Do While intX <udtItem.length AndAlso strSearchFor <> udtItem(intX).strId

intX = intX + 1

Loop

Page 22: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

22

Two-Dimensional Arrays

A two-dimensional array resembles a table in that the variables are in rows and columns

AC24 Shirt Red

BD12 Coat Blue

CP14 Blouse White

strProducts(0, 0) strProducts(0, 2)

strProducts(2, 1)

strProducts(1, 2)

Page 23: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

23

Two-Dimensional Arrays

Version 1

accessibility arrayname(highestRowSubscript, highestColumnSubscript) As datatype

Version 2

accessibility arrayname(,) As datatype = {{initialValues}, {initialValues}, …{initialValues}}

Dim strCitys(5, 3) As String

Dim intScores(,) As Integer = {{75, 90}, {9, 25}, {23, 56}, {6, 12}}

Page 24: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

24

Storing data in a Two-dimensional Array

Example 1

strCitys(0, 0) = “Madrid”

strCitys(0, 1) = “Paris”

strCitys(0, 2) = “Rome”

strCitys(0, 3) = “London”

Example 2

For intRow = 0 To 3

For intColumn = 0 To 1

intScores(intRow, intColumn) = 0

Next intColumn

Next intRow

Page 25: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

25

Calculating the Total in a Two-Dimensional Array

Dim intSales(,) As Integer = {{12000, 10000}, _

{45000, 56000}, {32000, 42000}, _

{67000, 23000}, {24000, 12000}, _

{55000, 34000}}

Dim intRow, intCol As Integer ‘keeps track of subscripts

Dim intTotal As Integer ‘accumulator variable

For intRow = 0 To 5

For intCol = 0 To 1

intTotal = intTotal + intSales(intRow, intCol)

Next intCol

Next intRow

Page 26: 1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare

26

Multidimensional Array Properties

Length will tell you the total number of elements intSales.Length will return 12

Use GetLength(dimension) to get the size of a particular dimension intSales.GetLength(0) will return 6 intSales.GetLength(2) will return 2

For intRow = 0 To intSales.GetLength(0) - 1 For intCol = 0 To intSales.GetLength(1) - 1 intTotal = intTotal + intSales(intRow, intCol) Next intColNext intRow