Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Terminology and Concepts

 

One-Dimensional Arrays                            Two-Dimensional Arrays

 

Arrays are data structures

They contain data - variables - of same type (Integer, Double, for example)

The size of an array (how many elements if contains) is fixed once it is created

 

 

An array named c with 12 elements

 

    c is the name of the array

 

    Position number is in parentheses - it must be positive integer - first element has position

    number 0

 

    c.Length is a method that returns the length of array c - which is 12 in this case

 

    c.GetUpperBound is a method that returns 1 less than the value of c.Length. The parameter 0

        indicates that it is a one-dimensional array - we discuss multiple dimensions later

 

Declaring and Creating Arrays

Arrays are objects that occupy memory

 

Either of the following statements can be used to declare an array

    Dim c As Integer()

    Dim c() As Integer

 

Created dynamically with keyword New

 

    Dim c As Integer() = New Integer(11){}

 

-Equivalent to:

    Dim c As Integer() ‘ declare array variable
    c = New Integer(11){} ‘ create array

 

-

Initializer List

 

Required braces {}

Specifies initial values of the elements in the array

When the initializer list is empty, the elements in the array are initialized to the default value for the type of elements of the array

The initializer list can contain a comma-separated list specifying the initial values of the elements in the array

Example

-    Dim c As Integer() = New Integer() {1, 2, 3, 4}

Summary

An array is a group of variables (called elements) that all have the same data type. The same array cannot hold both integers and doubles, for example.

To refer to a specific element of an array, specify the name of the array and its position number.

Every array begins with the zeroth element (element 0).

The position number in parentheses is called an index or a subscript.

Every array in VB knows its own length. The length of an array named A is returned by the expression A.length

Method GetUpperBound returns the index of the last element in the array. Note that A.GetUpperBound is 1 less than A.length

To declare an array, provide the array's name and type. For example

     Dim A as Integer().

     The parentheses that follow the type indicate that A is an array. Otherwise it would be considered a variable.

The declaration of an array creates a variable that can store a reference to any array but does not actually create the array in memory.

Before an array can be used you must specify the size and allocate memory for the array using the keyword New.

When the initializer list is empty, the elements in the array are initialized to the default value for the type of the elements in the array.

To pass an array to a method, specify the name of the array without using parentheses.

For a method to receive an array through its parameter list, the parameter must specify that an array is to be received.

Arrays are always passed by reference.

To pass an array element to a method, use the indexed name of the array element as an argument.

For a method to receive an array element through its parameter list, the parameter must specify that an array element is to be received.

The For loop is used to iterate through the elements of an array.