Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

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

3 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}

4 Array Declaration Examples Dim student(2) As String Dim 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”}

5 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

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

7 Direct Reference Versus Table Look Up Sometimes, 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 = 11 daysInNovember = monthTable(November-1) direct reference into monthTable

8 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 If Next index

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

10 Multidimensional Arrays To declare a two-dimensional array or table : Dim ArrayName(HighestRowSubscript, HighestColumnSubscript) As Datatype or Dim ArrayName(, ) As Datatype = {ListOfValues} Dim StudentNames(2,3) As String or Dim StudentNames(, ) As String = { {"James", "Mary", "Sammie", "Sean"}, {"Tom", "Lee", "Leon", "Larry"}, {"Maria", “Jane", "Jill", "John"} }

11 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

12 Example: Summing a Two-Dimensional Table

13 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 Col Next Row For Col As Integer = 0 To 5 For Row As Integer = 0 To 3 ColSum(Col) += amount(Row, Col) Next Row Next Col

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

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


Download ppt "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."

Similar presentations


Ads by Google