Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7: Working with Arrays

Similar presentations


Presentation on theme: "Chapter 7: Working with Arrays"— Presentation transcript:

1 Chapter 7: Working with Arrays
Visual Basic .NET Programming: From Problem Analysis to Program Design

2 Defining One-Dimensional Arrays
Consist of collection of elements Each element behaves as a variable does Can contain either: Primitive data Reference variables Visual Basic .NET Programming: From Problem Analysis to Program Design

3 Defining One-Dimensional Arrays (continued)
Elements must have same data type Access individual elements in array using index Can be one- or multi-dimensional Visual Basic .NET Programming: From Problem Analysis to Program Design

4 Defining One-Dimensional Arrays (continued)
Array class System namespace Provides properties and methods for working with arrays One-dimensional array Consists of elements arranged in single row Two-dimensional array Has both rows and columns Visual Basic .NET Programming: From Problem Analysis to Program Design

5 Defining One-Dimensional Arrays (continued)
Three-dimensional array Like a cube, with Rows Columns Pages Arrays are static Cannot change number of elements Visual Basic .NET Programming: From Problem Analysis to Program Design

6 Defining One-Dimensional Arrays (continued)
Declare array syntax: Dim arrayName (index of last element) As DataType Example: Dim examScores(4) As Integer Array indexes begin at zero Visual Basic .NET Programming: From Problem Analysis to Program Design

7 Defining One-Dimensional Arrays (continued)
Accessing array element syntax: arrayName(index) Example: examScores(0) Visual Basic .NET Programming: From Problem Analysis to Program Design

8 Visual Basic .NET Programming: From Problem Analysis to Program Design

9 Using a Loop to Iterate an Array
Use loop to process array elements Repeat program code for each element Especially appropriate when working with larger arrays Visual Basic .NET Programming: From Problem Analysis to Program Design

10 Example 7-3: Computing Exam Average Using an Array and a Loop
1. ' define variables 2. Dim examScores(4) As Integer 3. Dim sum, average As Double 4. Dim index As Integer Visual Basic .NET Programming: From Problem Analysis to Program Design

11 Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 7-3: Computing Exam Average Using an Array and a Loop (continued) 5. ' loop to enter and sum the exam scores 6. For index = 0 To examScores.Length - 1 Console.WriteLine("Enter an Exam Score: ") examScores(index) = Convert.ToInt32(Console.ReadLine()) sum += examScores(index) 10. Next Visual Basic .NET Programming: From Problem Analysis to Program Design

12 Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 7-3: Computing Exam Average Using an Array and a Loop (continued) 11. average = sum / examScores.Length 12. Console.WriteLine("The average is: " & Math.Round(average, 1)) Visual Basic .NET Programming: From Problem Analysis to Program Design

13 Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 7-3: Computing Exam Average Using an Array and a Loop (continued) Sample Run: Enter an Exam Score: 85 Enter an Exam Score: 90 Enter an Exam Score: 94 Enter an Exam Score: 89 Enter an Exam Score: 91 The average is: 89.8 Visual Basic .NET Programming: From Problem Analysis to Program Design

14 Invoking Array Methods
Selected Array class methods: Sort Sorts array elements Array.Sort(examScores) Reverse Rearranges contents of elements in reverse sequence Array.Reverse(examScores) Visual Basic .NET Programming: From Problem Analysis to Program Design

15 Creating String Arrays
Code to create String array: Dim stringArray(3) As String Each element in array: Reference variable Data type String Visual Basic .NET Programming: From Problem Analysis to Program Design

16 Visual Basic .NET Programming: From Problem Analysis to Program Design

17 Declaring Multi-Dimensional Arrays
Two-dimensional array Like table with rows and columns Three-dimensional array Like cube, with: Rows Columns Pages Visual Basic .NET Programming: From Problem Analysis to Program Design

18 Declaring Multi-Dimensional Arrays (continued)
Each array dimension has own index Generally arrays are one- or two-dimensional Declare two-dimensional array Similar to one-dimensional array declaration But specify number of rows and columns Example: Dim testScoreTable(4, 1) As Integer Visual Basic .NET Programming: From Problem Analysis to Program Design

19 Visual Basic .NET Programming: From Problem Analysis to Program Design

20 Visual Basic .NET Programming: From Problem Analysis to Program Design
Searching an Array Search array to see if it contains specific value Use loop to compare each value to target Use Boolean variable to mark found value Visual Basic .NET Programming: From Problem Analysis to Program Design

21 Example 7-10: Searching an Array (excerpt)
6. Do While i < stringArray.Length And Not found 7. If stringArray(i).Equals(searchValue) Then found = True 9. Else i += 1 11. End If 12. Loop Visual Basic .NET Programming: From Problem Analysis to Program Design

22 Using the ArrayList Class
Array elements are variables May be either: Primitive Reference variables Arrays have significant limitation: Fixed in size Difficult to change number of array elements as code is executing Visual Basic .NET Programming: From Problem Analysis to Program Design

23 Using the ArrayList Class (continued)
System.Collections namespace Use to create array that is dynamically resizable Visual Basic .NET Programming: From Problem Analysis to Program Design

24 Visual Basic .NET Programming: From Problem Analysis to Program Design

25 Example 7-11: Creating and Populating an ArrayList Instance (excerpt)
1. ' create an ArrayList instance with 3 elements 2. Dim anArrayList As ArrayList = New ArrayList(3) Visual Basic .NET Programming: From Problem Analysis to Program Design

26 Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 7-11: Creating and Populating an ArrayList Instance (continued) 8. ' populate the first two elements 9. anArrayList.Add(s1) 10. anArrayList.Add(s2) 11. Console.WriteLine(“number of elements = ” & anArrayList.Capacity) 12. Console.WriteLine(“populated elements = ” & anArrayList.Count) Visual Basic .NET Programming: From Problem Analysis to Program Design

27 Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 7-11: Creating and Populating an ArrayList Instance (continued) 13. ' iterate and display 14. Dim i As Integer 15. For i = 0 To anArrayList.Count - 1 Console.WriteLine(anArrayList.Item(i)) 17. Next Visual Basic .NET Programming: From Problem Analysis to Program Design

28 Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 7-11: Creating and Populating an ArrayList Instance (continued) Sample Run: number of elements = 3 populated elements = 2 Hello World Visual Basic .NET Programming: From Problem Analysis to Program Design

29 Visual Basic .NET Programming: From Problem Analysis to Program Design
Example 7-11: Creating and Populating an ArrayList Instance (continued) ArrayList properties: Capacity Contains number of elements Count Contains number of populated elements Visual Basic .NET Programming: From Problem Analysis to Program Design

30 Example 7-13: Invoking ArrayList Methods
Contains: anArrayList.Contains(s1) Returns True if ArrayList contains reference to argument IndexOf: anArrayList.IndexOf(s3) Returns index of argument in array Visual Basic .NET Programming: From Problem Analysis to Program Design

31 Example 7-13: Invoking ArrayList Methods (continued)
Reverse: anArrayList.Reverse() Rearranges contents of elements in reverse sequence Visual Basic .NET Programming: From Problem Analysis to Program Design

32 Visual Basic .NET Programming: From Problem Analysis to Program Design
Summary Arrays are instances of Array class Consist of elements with each element behaving as variable does Can be either: One-dimensional Multi-dimensional VB .NET supports multi-dimensional arrays Implemented by creating array of arrays Visual Basic .NET Programming: From Problem Analysis to Program Design

33 Visual Basic .NET Programming: From Problem Analysis to Program Design
Summary (continued) ArrayList class Dynamically resizable array Capacity property Contains number of elements in ArrayList Count property Contains number of populated elements For in-class practice: Ex. 1-4, 7; Pentagon Perimeter example. Homework: p. 248, PEx. 3 (use 4 1-dimentional arrays (flight1, flight2, etc.) instead of 1 2-dimentional array; input data dynamically (prompt user for data)) Visual Basic .NET Programming: From Problem Analysis to Program Design


Download ppt "Chapter 7: Working with Arrays"

Similar presentations


Ads by Google