Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 11 One-Dimensional Arrays. 2 Chapter 11 Topics l Atomic and composite data types l Declaring and instantiating an array l The length of an array.

Similar presentations


Presentation on theme: "1 Chapter 11 One-Dimensional Arrays. 2 Chapter 11 Topics l Atomic and composite data types l Declaring and instantiating an array l The length of an array."— Presentation transcript:

1 1 Chapter 11 One-Dimensional Arrays

2 2 Chapter 11 Topics l Atomic and composite data types l Declaring and instantiating an array l The length of an array l Manipulating the elements in an array l Using an array to count frequencies l Passing an array to a method

3 3 VB.NET Data Types Reference Array Interface Class primitive Integral Boolean Byte Char Short Integer Long Single Double Floating Point VB.NET Data Types

4 4 11.1 Scalar Data Type A scalar data type is a type in which l the values are ordered and each value is atomic (indivisible( 不可分割 )) Integer, Single, Double and Char data types are scalar.

5 5 Declare variables to store and total 3 blood pressures Dim bp0, bp1, bp2 As Integer Dim total As Integer bp1bp0bp2 Total = bp0 + bp1 + bp2

6 6 11.2 Composite Data Type A composite data type( 複合資料型態 ) is a type that l allows a collection of values to be associated with an identifier of that type. l In VB.NET, composite types are either classes, interfaces, or arrays. l There are 2 forms of composite types: unstructured and structured.

7 7 Structured Data Type( 結構化 ) A structured data type is a type which l is an organized collection of components; and allows individual components to be stored and retrieved. l The organization determines the method used to access individual components. l An array is a structured data type whose components are accessed by position.

8 8 What if you wanted to store and total 1000 values? Dim value(1000) As Integer ' declares and instantiates (creates) ' an array of 1000 int values ' and initializes all 1000 elements to zero value(0) value (1) value (2).... value (999) 0 0 0.... 0

9 9 11.3 Arrays( 陣列 ) Arrays are data structures( 資料結構 ) consisting of related data items all of the same type( 相同資料 型態 ). l An array type is a reference type. Contiguous memory locations( 連續記憶體位置 ) are allocated for the array, beginning at the base address of the array. l A particular element in the array is accessed by using the array name together with the position of the desired element in square brackets. The position is called the index or subscript( 索引 ).

10 10 angle Dim angle(4) As Single

11 11 Dim angle(4) As Single angle ( 0) angle ( 1) angle ( 2 ) angle ( 3 ) angle

12 12 Array Definition An array is a collection of elements, all of the same data type( 相同資料型態 ), given a single name. The subscript (or index) must have an integral value. In VB.NET, the first array element always has subscript 0. The second array element has subscript 1, etc. When allocated, the elements are automatically initialized to 0 for numeric primitive data type values, to False for Boolean variables, or to Nothing for references (non-primitive type values).

13 13 Another Example Declare and instantiate an array called angle to hold 4 individual double values. Dim angle ( 4 ) As Single ' declares and allocates memory angle(0) angle(1) angle(2) angle(3) number of elements in the array indexes or subscripts 0.0 0.0 0.0 0.0

14 14 Using an initializer list in a declaration Dim age( ) As Integer = { 23, 10, 16, 37, 12} Dim i As Integer For i = 0 to ages.Length – 1 Console.WriteLine("age("& i & ") = "& age(i) ) Next i age(0) age(1) age(2) age(3) age(4) 23 10 16 37 12

15 15 Dim ArrayName(Int Expression) As DataType Declaring and Allocating an Array l An array can be declared and allocated memory in one statement or an array can be declared in one statement and another statement ca be written later to allocate memory for the array, using Redim SYNTAX FORMS

16 16 Assigning values to individual array elements Dim angle(4) As Single ' creates array Dim m As Integer = 3 angle(0) = 4.93 angle(1) = -15.2 angle(2) = 0.5 angle(3) = 1.67 angle(m) = angle (3) – 1.2 angle(0) angle(1) angle(2) angle(3) 4.93 -15.2 0.5 1.67

17 17 Exmples angle(2) = 9.6 angle(2) = CDlb(inFile.ReadLine()) outFile.WriteLine(angle(2)) y = Math.Sqrt(angle(2)) x = 6.8 * angle(2) + 7.5

18 18 What values are assigned? Dim temps (4) As Double ' allocates array Dim m As Integer For m = 0 To temps.Length – 1 temps(m) = 100.0 + m * 0.2 Next temps(0) temps(1) temps(2) temps(3) temps(4) ? ? ? ? ?

19 19 What values are assigned? Dim temps (4) As Double ' allocates array Dim m As Integer For m = 0 To temps.Length – 1 temps(m) = 100.0 + m * 0.2 Next temps(0) temps(1) temps(2) temps(3) temps(4) ? ? ? ? ?

20 20 Now what values are printed? Const ARRAY_SIZE As Integer = 5 ' named constant Dim temps( ) As Double Redim temps(ARRAY_SIZE) Dim m As Integer For m = temps.Length – 1 To 0 Step –1 Console.WriteLine ("temps(“ & m & ") = " & temps(m)) temps(0) temps(1) temps(2) temps(3) temps(4) 100.0 100.2 100.4 100.6 100.8

21 21 Variable subscripts Dim temps(4) As Double Dim m As Integer = 3...... What is temps( m + 1 ) ? What is temps( m ) + 1 ? temps(0) temps(1) temps(2) temps(3) temps(4) 100.0 100.2 100.4 100.6 100.8

22 22 More about array index Array index can be an integral expression of type Char, Short, Byte, or Integer. l It is programmer’s responsibility to make sure that an array index does not go out of bounds. The index must be within the range 0 through the array’s length minus 1. Using an index value outside this range throws an IndexOutOfBoundsException. Prevent this error by using public instance method length.

23 23 Aggregate Array Operations numbers( 0) numbers( 1) numbers( 2 ) numbers values( 0) values( 1) values( 2 ) values Dim numbers( ) As Integer = {2, 4, 6} Dim values(3) As Integer values(0) = 2 values(1) = 4 values(2) = 6 If (numbers = values) then … 2 4 6 2 4 6

24 24 Aggregate Array Operations numbers( 0) numbers( 1) numbers( 2 ) numbers values( 0) values( 1) values( 2 ) values numbers = values If (numbers = values) then … 2 4 6 2 4 6

25 25 11.4 Examples of Declaring and Processing Arrays l Occupancy Rates Const BUILDING_SIZE as Integer = 350 Dim occupants(BUILDING_SIZE) AS Integer Dim totalOccupants As Inteer totalOccupants = 0 For counter = 0 To occupants.Length – 1 totalOccupants = totalOccupants +occupants(counter) Next counter occupants( 0) occupants( 1) occupants( 2 ).. occupants( 349 ) occupants

26 26 Sales Figures Dim gourmetBurgers (5) As Double gourmetBurgers( 0) gourmetBurgers( 1) gourmetBurgers( 2 ) gourmetBurgers( 3 ) gourmetBurgers( 4 ) gourmetBurgers 246.41 271.04 350.13 640.69 177.42

27 27 Using arrays for counters l Write a program to count the number of each alphabet letter in a text file. letterASCII ‘A’ 65 ‘B’ 66 ‘C’ 67 ‘D’ 68.. ‘Z’ 90 This is my text file. It contains many things! 5 + 8 is not 14. Is it? datafile.dat

28 28 Dim letterCount(25) As Integer counts ‘A’ and ‘a’ counts ‘B’ and ‘b’. counts ‘ Y’ and ‘y’ counts ‘Z’ and ‘z’ letterCount ( 0 ) 2 letterCount ( 1 ) 0.. letterCount ( 24) 1 letterCount ( 25 ) 0

29 29 Dim letter As Integer While (dataFile.Peek<> -1) letter = (Char(dataFile.Read ( ) ) If ((letter >= "A"c And letter <= "Z"c ) Or _ ((letter >= "a"c And letter <= "z"c ) Then index = (Int (Asc (letter.ToUpper(letter)) – Asc ("A")) letterCount(index) = letterCount (index) + 1 End If End While Counting Frequency of Alphabetic Characters

30 30 For index = 0 To letterCount.Length – 1 outFile.WriteLine ("The total number of " & _ Char(index + (Int (Asc ("A"))) & _ " " & letterCount (index)) Next Printing Frequency of Alphabetic Characters ' print each alphabet letter and its frequency count

31 31 Dim groceryItems (10) As String ( 0 ) “cat food” ( 1 ) “rice”.. ( 8 ) “spinach” ( 9 ) “butter” groceryItems 11.5 Array Of Objects (Strings)

32 32 Dim groceryItems(10) As String ( 0 ) “cat food” ( 1 ) “rice”.. ( 8 ) “spinach” ( 9 ) “butter” groceryItems Expression Class/Type groceryItems Array groceryItems(0) String groceryItem(0).Chars(0) Char

33 33 11.6 Passing Arrays as Arguments l In VB.NET an array is a reference type. What is passed to a method with an array parameter is the address of where the array object is stored. The name of the array is actually a reference to an object that contains the array elements and the public instance variable Length.

34 34 Public Function sumSales (ByRef data( ) As Integer) As Integer Dim sum As Double = 0 Dim index As Integer For index = 0 To data.Length – 1 sum = sum + data ( index ) Next index Return sum End Function Passing an Array as Parameter

35 35 Passing an Array as Parameter Dim gourmetBurgers (5) As Double outFile.WriteLine (sumSales(gourmetBurgers)) gourmetBurgers( 0) gourmetBurgers( 1) gourmetBurgers( 2 ) gourmetBurgers( 3 ) gourmetBurgers( 4 ) gourmetBurgers 246.41 271.04 350.13 640.69 177.42


Download ppt "1 Chapter 11 One-Dimensional Arrays. 2 Chapter 11 Topics l Atomic and composite data types l Declaring and instantiating an array l The length of an array."

Similar presentations


Ads by Google