arrays an array is a list or series of values all referenced by the same name also referred to as a...

15
Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array A subscript or index is a zero-based integer used to reference the specific elements in the array (0) (1) (2) (3) (4) (5) (6) (7) (8) (9) Janet Baker George Lee Sue Li Samuel Hoosier Sandra Weeks William Macy Andy Harrison Ken Ford Denny Franks Shawn James studentName

Upload: meghan-fields

Post on 18-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

The Declaration Statements for Arrays Arrays can be declared using Dim, Public, Private or Friend. Numeric variables are initialized to 0 and string elements to the empty string. Three forms are available: Important Note: You can declare an array with an upper bound or an initial value list, but not both. Dim ArrayName(UpperSubscript) As Datatype Dim ArrayName( ) As Datatype = {InitialValueList} Dim ArrayName As Datatype( ) = {InitialValueList}

TRANSCRIPT

Page 1: Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array

Arrays

• An array is a list or series of values all referenced by the same name

• Also referred to as a table

• An element is an individual item in the array

• A subscript or index is a zero-based integer used to reference the specific elements in the array

(0)(1)(2)(3)(4)(5)(6)(7)(8)(9)

Janet BakerGeorge LeeSue LiSamuel HoosierSandra WeeksWilliam MacyAndy HarrisonKen FordDenny FranksShawn James

studentName

Page 2: Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array

Array Subscripts

• Subscripts may be numeric constants, numeric variables, or numeric expressions.

• Subscripts must be integers — VB rounds any non-integer subscript.

• Arrays are bounded by the lower subscript, which is always 0, and the upper subscript.

• VB throws exceptions for subscripts that are out of range.

• The size of the array is one greater than the upper bound.

Page 3: Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array

The Declaration Statements for Arrays

• Arrays can be declared using Dim, Public, Private or Friend.

• Numeric variables are initialized to 0 and string elements to the empty string.

• Three forms are available:

• Important Note: You can declare an array with an upper bound or an initial value list, but not both.

Dim ArrayName(UpperSubscript) As DatatypeDim ArrayName( ) As Datatype = {InitialValueList}Dim ArrayName As Datatype( ) = {InitialValueList}

Page 4: Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array

Array Declaration Examples

Dim student(2) As StringDim student() As String = {“John”, “Mary”, “Tom”}Dim student As String() = {“John”, “Mary”, “Tom”}

These 3 statements CORRECTLY declare an array of 3 elements.

This statement INCORRECTLY declares an array of 3 elements.

Dim student(2) As String = {“John”, “Mary”, “Tom”}

Page 5: Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array

For Each/Next Statement• Use loops to reference each element in the array.

• For Each/ Next makes one pass through the loop per element

• The looping variable ElementName must be same type as the array.

For Each ElementName [As Datatype] In ArrayName ' Statement(s) in loop.

Next [ElementName]

Dim students() As String = {“John”, “Mary”, “Tom”}For Each OneName As String In students

Debug.WriteLine(OneName)Next OneName

Page 6: Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array

Debugging Arrays

• View the array elements in debugging time by setting a breakpoint and view the Autos window; click the plus sign to left of array name to view individual array elements.

Page 7: Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array

Direct Reference Versus Table Look UpSometimes, if the information is sequential, you can use the information provided by the user as an index into an array. This is known as a direct reference.

Dim monthTable() As Integer = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}Dim November, daysInNovember As Integer

November = 11daysInNovember = monthTable(November-1)

direct reference into monthTable

Page 8: Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array

Direct Reference Versus Table Look Up (2)Other times, if the information is not sequential, you must use the information provided by the user indirectly through a table look up to get an index into an array.

Dim socialSecurityNumber() As Integer = {222334444, 555667777, 111889999, 333006666}Dim employeeName() As String = {“A. Smith”, “B. Jones”, “C. Davis”, “D. Johnson”}

For index As Integer = 0 to 3 If socialSecurityNumber(index) = Integer.Parse(ssTextbox.Text) Then Debug.WriteLine(employeeName(index))

Exit For End IfNext index

Page 9: Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array

Using List Boxes With Arrays

Index = GroupListBox.SelectedIndex

Allow the user to select from a list and the SelectedIndex property can be used as the subscript of the Sales array.

Table look up is easy when a list box is used on the form.

Page 10: Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array

Multidimensional Arrays

To declare a two-dimensional array or table :

Dim ArrayName(HighestRowSubscript, HighestColumnSubscript) As Datatype orDim ArrayName( , ) As Datatype = {ListOfValues}

Dim StudentNames(2,3) As String orDim StudentNames( , ) As String = { {"James", "Mary", "Sammie", "Sean"}, {"Tom", "Lee", "Leon", "Larry"}, {"Maria", “Jane", "Jill", "John"} }

Page 11: Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array

The Previous Example Sets Up This 2D Array

(0, 0)James

(0, 1)Mary

(0, 2)Sammie

(0, 3)Sean

(1, 0)Tom

(1, 1)Lee

(1, 2)Leon

(1, 3)Larry

(2, 0)Maria

(2, 1)Jane

(2, 2)Jill

(2, 3)John

Column 0 Column 1 Column 2 Column 3

Row 0

Row 1

Row 2

Page 12: Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array

Example: Summing a Two-Dimensional Table

Page 13: Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array

Code for Summing 2D Array

Dim RowSum() As Decimal = {0D, 0D, 0D, 0D}Dim ColSum(5) As Decimal

For Row As Integer = 0 To 3 For Col As Integer = 0 To 5 RowSum(Row) += amount(Row, Col)

Next ColNext Row

For Col As Integer = 0 To 5 For Row As Integer = 0 To 3 ColSum(Col) += amount(Row, Col) Next RowNext Col

Page 14: Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array

.NET Collections• Collections are more fully powered arrays used to handle a group of related data.

• Collections aren’t fixed length, unlike arrays.• Collections provide special object-oriented features (methods) for doing things

like:• adding/ deleting items• using keys, not just indices, for accessing items• sorting/ clearing the collection, etc.

• Example collections available in System.Collections• ArrayList• SortedList• Queue• Stack

Page 15: Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array

SortedList Collection• Elements automatically sorted.• Items are key – value pairs• Elements can be referenced by key or index.• Features many methods to aid in data handling.