UCF homepage
Lars P. Linden
MIS Dept.
University of Central Florida


Lab on Arrays

concepts that are covered:




Array Syntax

This is the syntax.

This is how to declare a one-dimensional array

Dim arrayName(upperbound) As type

Doesn't seem like much, right? What is missing here is an exclamation of the stupendous importance of adding those parentheses.




Three Basic Data Structures



Understand in the importance of arrays in terms of different data structures.

Up until now we have dealt with variables that have singular values. Regular variables are atomic. Variables that are arrays contain a series of values.




Upperbound

The first question is: "How many?"


The trick is that upperbound of an array is zero-based.

To create an array of holds 100 values, the upperbound should be 99.

Dim numbers(99) As Integer





Index

Given an array, you assign values to the memory associated with that array by specifying the index.

' given this array Dim strTechCompanies(2) As String

Think of the index as the "address" to that memory spot within the array's memory structure.

We call a particular this memory spot an element.


Below is an example. The indexes are 0, 1, and 2

' use an index to assign values to particular elements strTechCompanies(0) = "IBM" strTechCompanies(1) = "Microsoft" strTechCompanies(2) = "Google"



Getting an element from an array

Use the index to obtain the value of an element.

lblCompany.Text = strTechCompanies(2)





Length of an Array

Often you need to know how many elements an array has.

Use the Length property.

strTechCompanies.Length

Knowing the length will be important later on when we start changing the size of our arrays.






Using Indexes that Do Not Exist

Here is the primary bug that you want to avoid when you code with arrays.

If you reference an index that does not exist for the array, the program throws an IndexOutOfRangeException

' will crash strTechCompanies(3) = "Vaporware"


This should be a primary concern when you code with arrays.




Creating an array and assigning values in one statement

If you have the values to be assigned to the array, you can declare the array and assign the values all in one statement.

An example:

Dim intCashFlows() As Integer = {5000, 2000, 8000, 9000}

Big heads-up. Note that the upperbound is not specified above.


The syntax:

Dim arrayName() As type = [New type(upperbound)] {value1[, value2] [, value3]...}



A Concept Map of an Array

Here is an illustration of an array in memory.





Summary of Array Basics

Here is what was covered:










<< back to course home