1 / 17

Arrays and Collections

Arrays and Collections. By: Engr. Faisal ur Rehman CE-105 Spring 2007. Array. Definition. A variable storing multiple values Called Matrix in Mathematics Alternatives are: Collection Scalar variables. Array Terms. ‘declaration Dim a(5) as integer. Data type. Name same for all

Download Presentation

Arrays and Collections

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays and Collections By: Engr. Faisal ur Rehman CE-105 Spring 2007

  2. Array

  3. Definition • A variable storing multiple values • Called Matrix in Mathematics • Alternatives are: • Collection • Scalar variables

  4. Array Terms ‘declaration Dim a(5) as integer Data type Name same for all varaibles in this array Index or subscript

  5. Array Terms • Dimension: Specify the direction in which variable changes. • 1d = n x 1 Dim a(2) as double • 2d = m x n Dim a(2,2) as double • 3d = l x m x n Dim a(2,2,2) as double • Max 32 dimensions • More than 3 are rare • Rank: The Rank property returns the array's rank (number of dimensions).

  6. Array Terms 2 1 0 3D 3x3x3 1D 3x1 2D 3x3 • Dimensions

  7. Array Terms • The GetLength method returns the length along the specified dimension • Def: Represents the number of elements in the specified dimension of the Array. • GetUpperBound (method): Gets the upper bound of the specified dimension in the Array • The Length property returns the total number of elements in the array. • The System.Array.Sort method sorts the elements of a one-dimensional array.

  8. Array Terms • Jagged Array (Array of Array): Sometimes the data structure in your application is two-dimensional but not rectangular. • For example, you might have an array of months, each element of which is an array of days. • In such a case, you can use a jagged array instead of a multidimensional array.

  9. Array Terms • The size of an array is the product of the lengths of all its dimensions. • It represents the total number of elements currently contained in the array. Example: Dim prices(3, 4, 5) As Long • The overall size of the array in variable prices is (3 + 1) x (4 + 1) x (5 + 1) = 120. • Determined by length property

  10. Array Term • Reallocates storage space for an array variable. • Used for dynamic array (change length at runtime)An array with no elements is also called a zero-length array • You can initialize an array at the same time you create it. • Dim P(3) as double() = New double() {2, 4, 5, 6}

  11. Collections

  12. Collection • Collection is a data structure used for object • More efficient in storing items than an array.

  13. Collection Index and Key Values • Instances of the Visual Basic Collection class allow you to access an item using either: • a numeric index -or- • a String key. • You can add items to Visual Basic Collection objects either with or without specifying a key. • If you add an item without a key, you must use its numeric index to access it. Item of Collection: A single / element of collection

  14. Collection Example Dim birthdays As New Collection() birthdays.Add(New DateTime(2001, 1, 12), "Bill") birthdays.Add(New DateTime(2001, 1, 13), "Joe") birthdays.Add(New DateTime(2001, 1, 14), "Mike") birthdays.Add(New DateTime(2001, 1, 15), "Pete") Dim aBirthday As DateTime aBirthday = birthdays.Item("Bill") MsgBox(CStr(aBirthday)) aBirthday = birthdays("Bill") MsgBox(CStr(aBirthday)) End Sub

  15. Q & A

  16. Q & A(Assignment) • Define: • Array • Size of Array • Length of Array • Rank of Array • Dimension of Array • System.array.sort • Jagged Array • Dynamic array • Define: • Collection • Key • Index • Item

  17. Thanks

More Related