1 Chapter 11 One-Dimensional Arrays. 2 Chapter 11 Topics l Atomic and composite data types l...

35
1 Chapter 11 One-Dimensional Arrays

Transcript of 1 Chapter 11 One-Dimensional Arrays. 2 Chapter 11 Topics l Atomic and composite data types l...

Page 1: 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.

1

Chapter 11

One-Dimensional Arrays

Page 2: 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.

2

Chapter 11 Topics

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

Page 3: 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.

3

VB .NET Data TypesVB .NET Data Types

Reference

Array Interface Class

primitive

Integral Boolean

Byte Char Short Integer Long Single Double

Floating Point

VB .NET Data Types

Page 4: 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.

4

11.1 Scalar Data Type

A scalar data type is a type in which

the values are ordered and each value is atomic (indivisible(不可分割 ))

Integer, Single, Double and Char data types are scalar.

Page 5: 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.

5

Declare variables to store and total 3 blood pressures

Dim bp0, bp1, bp2 As Integer

Dim total As Integer

bp1bp0 bp2

Total = bp0 + bp1 + bp2

Page 6: 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.

6

11.2 Composite Data Type

A composite data type(複合資料型態 ) is a type that

allows a collection of values to be associated with an identifier of that type.

In VB .NET, composite types are either classes, interfaces, or arrays.

There are 2 forms of composite types: unstructured and structured.

Page 7: 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.

7

Structured Data Type(結構化 )

A structured data type is a type which

is an organized collection of components; and allows individual components to be stored and retrieved.

The organization determines the method used to access individual components.

An array is a structured data type whose components are accessed by position.

Page 8: 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.

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

Page 9: 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.

9

11.3 Arrays(陣列 )

Arrays are data structures(資料結構 ) consisting of related data items all of the same type(相同資料型態 ).

An array type is a reference type. Contiguous memory locations(連續記憶體位置 ) are allocated for the array, beginning at the base address of the array.

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(索引 ).

Page 10: 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.

10

angle

Dim angle(4) As Single

Page 11: 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.

11

Dim angle(4) As Single

angle ( 0)

angle ( 1) angle ( 2 )

angle ( 3 )

angle

Page 12: 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.

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).

Page 13: 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.

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

Page 14: 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.

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

Page 15: 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.

15

Dim ArrayName(Int Expression) As DataType

Declaring and Allocating an Array

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

Page 16: 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.

16

Assigning values to individual array elements

Dim angle(4) As Single ' creates arrayDim m As Integer = 3angle(0) = 4.93angle(1) = -15.2angle(2) = 0.5angle(3) = 1.67angle(m) = angle (3) – 1.2

angle(0) angle(1) angle(2) angle(3)

4.93 -15.2 0.5 1.67

Page 17: 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.

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

Page 18: 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.

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)

? ? ? ? ?

Page 19: 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.

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)

? ? ? ? ?

Page 20: 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.

20

Now what values are printed?

Const ARRAY_SIZE As Integer = 5 ' named constantDim temps( ) As DoubleRedim temps(ARRAY_SIZE)Dim m As Integer

For m = temps.Length – 1 To 0 Step –1Console.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

Page 21: 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.

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

Page 22: 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.

22

More about array index

Array index can be an integral expression of type Char, Short, Byte, or Integer.

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.

Page 23: 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.

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 Integervalues(0) = 2values(1) = 4values(2) = 6

If (numbers = values) then…

2

4

6

2

4

6

Page 24: 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.

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

Page 25: 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.

25

11.4 Examples of Declaring and Processing Arrays

Occupancy RatesConst 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

Page 26: 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.

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

Page 27: 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.

27

Using arrays for counters

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

Page 28: 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.

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

Page 29: 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.

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

Page 30: 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.

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

Page 31: 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.

31

Dim groceryItems (10) As String

( 0 ) “cat food”

( 1 ) “rice”

. .

. .

. .

( 8 ) “spinach”

( 9 ) “butter”

groceryItems

11.5 Array Of Objects (Strings)

Page 32: 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.

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

Page 33: 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.

33

11.6 Passing Arrays as Arguments

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.

Page 34: 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.

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

Page 35: 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.

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