170 likes | 267 Views
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
E N D
Arrays and Collections By: Engr. Faisal ur Rehman CE-105 Spring 2007
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 varaibles in this array Index or subscript
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).
Array Terms 2 1 0 3D 3x3x3 1D 3x1 2D 3x3 • Dimensions
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.
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.
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
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}
Collection • Collection is a data structure used for object • More efficient in storing items than an array.
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
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
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