Home

Arrays

 

Arrays Fundamentals

 

Introduction

To use a group of values in a program, we usually declare different variables for them, with each variable representing one of the values. When creating such a program, if you have a series of values of the same type, you can use a single variable that would represent all of those values. Such a variable is called an array.

An array is a series of values grouped under one variable.

Array Declaration

To declare an array, use the var keyword, specify the type of values that will be stored in the array using a data type. To indicate that you are declaring an array, the data type must be followed square brackets. The formula used is:

var VariableName : DataType[];

As you can see, there are two primary piece of information you must provide: a name for the variable and the data type. When declaring an array, you can initialize it as we will see in the next section. Sometimes when declaring an array, you may not have the values of its elements, which means you cannot initialize it. To declare such an array, you must assign the new operator, followed by the same data type used to declare the array, followed by square brackets. Inside of this second pair of square brackets, you must type the number of elements that the array will have. Here is an example:

var Number : int[] = new int[5];

This declaration makes the array ready.

Array Initialization

When declaring an array variable, you must decide whether you are ready to specify the values of its elements. Specifying these values is also referred to as initializing the array. There are two techniques you can use.

If you want to initialize an array when declaring it, between the data type and the semi-colon, type the assignment operator "=" followed by the list of elements separated by commas. Here is an example:

var Number : int[] = [5, 86, 485, -136, 2593];

The elements that compose an array are also referred to as its members.

 

Access to Array Members

As mentioned already, an array is a variable whose name represents a series of values. Therefore, each member of the array holds its own value. You can access each member of the array and manipulate it. To make this possible, each member of the array is referred to using an index. The first member of the array has an index of 0. The second has an index of 1, etc. The last member of the array has an index of n-1 with n representing the number of elements. Based on this, to access the third value of the above Number array, you would use Number[2].

After accessing a member of the array, you can initialize it. Here is an example:

var Number : int[] = new int[5];

Number[0] = 5;
Number[1] = 86;
Number[2] = 485;
Number[3] = -136;
Number[4] = 2593;

In the same way, you display the value of an element by passing it to print(). Here is an example:

var Number : int[] = new int[5];

Number[0] = 5;
Number[1] = 86;
Number[2] = 485;
Number[3] = -136;
Number[4] = 2593;

print(Number[2]);

Arrays and Classes

 

The Array Class

The techniques we have used so far to create and use arrays are the same employed in C/C++, C#, and other similar languages. To provide a formal support of arrays, the JScript language provides the Array class used to declare and explore arrays. Based on this class, you can declare an array using the default constructor of this class. The formula to use is:

var VariableName = new Array();

The only new thing in this formula is the name of the class Array to use. Here is an example:

var Distance = new Array();

When you use this formula, you are said to declare an array object. Leaving the parentheses empty indicates to the compiler that you will add members as you judge it necessary. With this type of array, you can add as many members as you want. To do this, type the name of the variable followed by its square brackets. Inside of the brackets, type an index number. The first index must be 0, the second 1, and so on. This indexing allows you to access a member of the array. You can then assign it a value. In the same way you can display the value of a member of the array. Here is an example:

var Distance = new Array();

Distance[0] = 125.58;
Distance[1] = 87.24;
Distance[2] = 125.58;
Distance[3] = -44.70;
Distance[4] = 6002.96;
Distance[5] = 824.024;

print(Distance[0]);
print(Distance[1]);
print(Distance[2]);
print(Distance[3]);
print(Distance[4]);
print(Distance[5]);

This would produce:

125.58
87.24
125.58
-44.7
6002.96
824.024

If you declare an array object using the Array constructor and you leave the parentheses of the constructor empty, the array would be empty at first. When you start initializing the members of the array, the constructor would keep track of the number of members you have initialized until you stop. This number would be the number of members of the array. For example, the above Distance array has 6 members because we initialized 6 members. If you try manipulating a member with an index beyond the count-1 of members that were initialized, that member would have an undefined value. Consider the following:

var Distance = new Array();

Distance[0] = 125.58;
Distance[1] = 87.24;
Distance[2] = 125.58;
Distance[3] = -44.70;
Distance[4] = 6002.96;
Distance[5] = 824.024;

print(Distance[0]);
print(Distance[1]);
print(Distance[2]);
print(Distance[3]);
print(Distance[4]);
print(Distance[5]);
print(Distance[6]);
print(Distance[7]);

This would produce:

125.58
87.24
125.58
-44.7
6002.96
824.024
undefined
undefined

This indicates that, with a compiled language like JScript .NET, you should always specify the number of members of an array when declaring it. In fact, many languages don't allow you to access a member of an array beyond the indexed number.

When using the Array class, you can also create an array and specify the number of members it will contain. To do this, when creating the array, type an integer in the parentheses of the constructor of the Array class. Here is an example:

var Distance = new Array(6);

After declaring such an array, you can access each member using the square brackets and specifying its index. Here is an example:

var Distance = new Array(6);

Distance[0] = 125.58;
Distance[1] = 87.24;
Distance[2] = 125.58;
Distance[3] = -44.70;
Distance[4] = 6002.96;
Distance[5] = 824.024;

print(Distance[0]);
print(Distance[1]);
print(Distance[2]);
print(Distance[3]);
print(Distance[4]);
print(Distance[5]);

An Array of a Class

The arrays we have used so far were made for primitive data types. If you create a class, you can declare an array of it, using the same techniques we have used so far. Image you create a class that represents the items of a department store as follows:

class StoreItem
{
    var ItemNumber : long;
    var ItemName   : String;
    var UnitPrice  : double; 
}

To declare an array of this class, you can proceed as follows:

class StoreItem
{
    var ItemNumber : long;
    var ItemName   : String;
    var UnitPrice  : double; 
}

var anItem : StoreItem[] = new StoreItem[2];

An array declared from a class is a variable like any other. Each one of its members can be identified by its index. Once an index is specified, the array member has its own set of member variables. Based on this, you can access each member of the class using the index of the array variable. Here is an example:

class StoreItem
{
    var ItemNumber : long;
    var ItemName   : String;
    var UnitPrice  : double; 
}

var anItem : StoreItem[] = new StoreItem[2];

anItem[0] = new StoreItem();
anItem[0].ItemNumber = 79576;
anItem[0].ItemName   = "Women Skirt";
anItem[0].UnitPrice  = 34.55;

anItem[1] = new StoreItem();
anItem[1].ItemNumber = 305417;
anItem[1].ItemName   = "60\" Gold Chain";
anItem[1].UnitPrice  = 224.95;

print("Store Inventory");
print("Item # " + anItem[0].ItemNumber);
print("Name:  " + anItem[0].ItemName);
print("Price: $" + anItem[0].UnitPrice);
print();
print("Item # " + anItem[1].ItemNumber);
print("Name:  " + anItem[1].ItemName);
print("Price: $" + anItem[1].UnitPrice);

 

 

Previous Copyright 2004 FunctionX, Inc. Next