11
7 – Arrays Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB .NET Programming

7 – Arrays Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

Embed Size (px)

Citation preview

Page 1: 7 – Arrays Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

7 – Arrays

Lingma AchesonDepartment of Computer and Information Science, IUPUI

CSCI N331 VB .NET Programming

Page 2: 7 – Arrays Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

Represent A List of Values• Sometimes we need to represent a list

of values that have the similar characteristics.

• Example 1: Quarterly sales figures of one year. We can use four variables to store them.

dblQuarter1 = 12.8dblQuarter2 = 15.6dblQuarter3 = 23.2dblQuarter4 = 18.8

2

Page 3: 7 – Arrays Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

• Example 2: Names of the products that a vending machine carries. We can use six variables if there are six of them.

strName1 = “Pepsi”strName2 = “Sprite”strName3 = “Root Beer”strName4 = “Diet Coke”strName5 = “Mountain Dew”strName6 = “7 up”

3

Represent A List of Values

Page 4: 7 – Arrays Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

• Example 3: Exam scores of each student in a class. If there are 28 students in the class, do we need 28 variables? Is there a better way?

intScore1 = 97intScore2 = 68intScore3 = 82intScore4 = 91intScore5 = 85intScore6 = 88intScore7 = 72….

4

Represent A List of Values

Page 5: 7 – Arrays Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

What is an array?• Yes, use an array!• Array - A data structure that holds a list

of values of the same type. E.g.– Quarterly sales, an array of 4 values of type Double:

– Student scores, an array of 28 values of type Integer:

– Name of products in a vending machine, an array of 6 values of type String:

5

12.8 15.6 23.2 18.8

97 68 82 91 85 88 72 73 … 88

Coke Sprite Root Beer

Diet Coke

Mountain Dew

7 up

Page 6: 7 – Arrays Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

What is an array?• Caution – each structure is just one

variable! That variable can hold multiple values.

• E.g.– Quarterly sales:

Name of the array: QuarterlySales

How many values does the array have: 4

Type of the values: Double– Name of products in a vending machine

Name of the array: Products

How many values does the array have: 6

Type of the values: String6

12.8 15.6 23.2 18.8

Coke Sprite Root Beer

Diet Coke

Mountain Dew

7 up

Page 7: 7 – Arrays Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

Values in an Array• Now we have a name for the whole

structure, how do we refer to each value? E.g. how do I know the sales figure for the third quarter?

• Answer: Use position to identify a value as array values are stored in order. – E.g. QuarterlySales:

1st element of QuarterlySales: 12.8

2nd element of QuarterlySales: 15.6

3rd element of QuaterlySales: 23.2

4th element of QuarterlySales: 18.8

7

12.8 15.6 23.2 18.8

Page 8: 7 – Arrays Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

Values in an Array• How do we use VB code to specify a

position? Use array indexes that start from 0.E.g. QuarterlySales(0): 12.8

QuarterlySales(1): 15.6

QuarterlySales(2): 23.2

QuarterlySales(3): 18.8

E.g.‘Initialize all the values to 0

For n = 0 To 3

QuarterlySales(n) = 0

Next8

12.8 15.6 23.2 18.8

Page 9: 7 – Arrays Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

How to create an array?• Basic structure:

Dim arrayname As datatype()or:Dim arrayname() As datatypeE.g. ‘delare an array called dblQuarterlySales that holds Double valuesDim dblQuarterlySales As Double()

9

Page 10: 7 – Arrays Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

How to create an array?• Variations

‘declare an array with 3 as upperbound index.‘an array of 4 elements.Dim dblQuarterlySales As Double(3)

‘initialize values when declaring the array, no need to specify a size, as four values indicate a size of 4Dim dblQuarterlySales As Double() = {12.8, 15.6, 23.2, 18.8}

10

Page 11: 7 – Arrays Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

Retrieve Values from an Array• E.g

‘get the first element from the array and assign it to ‘a variabledblQuarterOne = dblQuarterlySales(0)

‘display result in a textboxtxtOutput.Text = “Last quarter sale: “ & _

Cstr(dblQuarterlySales(3))

‘display all the values in a listboxFor n = 0 To 3

lstOutput.Items.Add(Cstr(dblQuarteylySales(n)))

Next

11