1 / 17

# Arrays and Collections - PowerPoint PPT Presentation

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Arrays and Collections' - urban

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Arrays and Collections

By: Engr. Faisal ur Rehman

CE-105 Spring 2007

• A variable storing multiple values

• Called Matrix in Mathematics

• Alternatives are:

• Collection

• Scalar variables

‘declaration

Dim a(5) as integer

Data

type

Name same for all

varaibles in this array

Index or subscript

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

2

1

0

3D

3x3x3

1D

3x1

2D

3x3

• Dimensions

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

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

• 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

• 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 is a data structure used for object

• More efficient in storing items than an array.

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

Dim birthdays As New Collection()

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