Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 3327 Visual Basic Chapter 7: Arrays

Similar presentations


Presentation on theme: "CSCI 3327 Visual Basic Chapter 7: Arrays"— Presentation transcript:

1 CSCI 3327 Visual Basic Chapter 7: Arrays
UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous lecture slides. – Xiang Lian

2 Objectives In this chapter, you will:
Learn how arrays are used to store elements Get familiar with the declaration, initialization, and reference to elements of the array Know how to pass arrays to methods Learn how to use For Each … Next statement to iterate through elements in the array

3 Arrays An array is a group of variables (called elements) containing values that all have the same data type For most languages, the number of elements are static and defined at declaration In Visual Basic, arrays can be re-dimensioned at the execution time Single dim, two dim and multiple dim

4 Example of an Array The first element is the zeroth element
C(0) -45 C(1) 6 C(2) C(3) 72 C(4) 34 C(5) 39 C(6) 98 C(7) -1345 C(8) 939 C(9) 10 C(10) 40 C(11) 33 The first element is the zeroth element The highest position number is array’s upper bound Array C on RHS: 11 Access array elements Use index (or subscript) E.g., C(8)

5 Declaring an Array Declaration (different ways)
Dim Grades As Integer() Grades = New Integer(0 to 11) {} {} initializer list Grades = New Integer(11) {} Dim Grades() As Integer Declare and initialize with initializer lists Dim Grades() As Integer={90,80,88,90,70} Local type inference Dim Grades ={90,80,88,90,70}

6 Default Initialization
When you do not provide initializer list, elements are initialized to default values Numeric primitive data type: 0 Boolean data type: False String data type: Nothing

7 Referring to an Element
Name and position Grades(6) = 85 First element is 0th index (subscript) Index must be a nonnegative integer Index may have a calculated value like Grades(i+4*2) Know the difference between index and value stored there Sum = Grades(1) + Grades(2) + Grades(3)

8 Array is a Class Class is in System.Array X = C.GetUpperBound(0)
-45 C(1) 6 C(2) C(3) 72 C(4) 34 C(5) 39 C(6) 98 C(7) -1345 C(8) 939 C(9) 10 C(10) 40 C(11) 33 Class is in System.Array X = C.GetUpperBound(0) Will give 11 (see the declaration) X= C.GetLength(0) Will give 12 X=C.Length

9 Example 7.2: InitializeArrays.vb
URL: GetUpperBound(0)

10 Example 7.3: SumArray.vb URL: For i = 0 To values.GetUpperBound(0)
For i = 0 To values.GetUpperBound(0) Total+= values(i) Next

11 Example 7.4: StudentPoll.vb
URL: IndexOutOf Range Exception Try … statements Catch ex As IndexOutOfRangeException 'handling exceptions End Try

12 Example 7.5: DiceStatistics.vb
URL: Summarize statistics of the dice CType

13 Example 7.6: Flag Quiz URL: Properties of ComboBox
Properties of ComboBox DropDownStyle: DropDownList (not editable) MaxDropDownItems: the maximum number of items displayed at one time CountriesComboBox.DataSource=countries 'array CountriesComboBox.Selected Index = 0 Replace method of String: country.Replace(" ","")

14 Passing Arrays Always as byVal (it is just a pointer to the 0th element) Declaration Dim scores(80) As Integer Function standardDeviation(ByVal scores() As Integer, ByVal numscores As Integer) As Double Call stdDev = standardDeviation(scores, numScores)

15 Manipulating Array Using Loops
For i = 0 To scores.GetUpperBound(0) Total += scores(i) Next

16 For Each … Next Statement
For Each grade In gradeArray Total += grade Next For Each grade As Integer In gradeArray

17


Download ppt "CSCI 3327 Visual Basic Chapter 7: Arrays"

Similar presentations


Ads by Google