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.8 dblQuarter2 = 15.6 dblQuarter3 = 23.2 dblQuarter4 =
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
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 = 97 intScore2 = 68 intScore3 = 82 intScore4 = 91 intScore5 = 85 intScore6 = 88 intScore7 = 72 …. 4 Represent A List of Values
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: …88 CokeSpriteRoot Beer Diet Coke Mountain Dew 7 up
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: String CokeSpriteRoot Beer Diet Coke Mountain Dew 7 up
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: 1 st element of QuarterlySales: nd element of QuarterlySales: rd element of QuaterlySales: th element of QuarterlySales:
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 Next
How to create an array? Basic structure: Dim arrayname As datatype() or: Dim arrayname() As datatype E.g. ‘delare an array called dblQuarterlySales that holds Double values Dim dblQuarterlySales As Double() 9
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 4 Dim dblQuarterlySales As Double() = {12.8, 15.6, 23.2, 18.8} 10
Retrieve Values from an Array E.g ‘get the first element from the array and assign it to ‘a variable dblQuarterOne = dblQuarterlySales(0) ‘display result in a textbox txtOutput.Text = “Last quarter sale: “ & _ Cstr(dblQuarterlySales(3)) ‘display all the values in a listbox For n = 0 To 3 lstOutput.Items.Add(Cstr(dblQuarteylySales(n))) Next 11