Arrays

I want to do a calculation on scores obtained by contestants in a competition. The scores are a set of 7 integer numbers. They are 110, 170, 870, 70, 350, 240 and 330. I could make 7 variables like:
var score1 = 110;
var score2 = 170;
and so on. Hmm. In case I end up with 300 scores, maybe it would be better to use an array.

Go to http://haxe.org/api and look at Array. Note there are some functions built-in to Haxe available to operate on arrays. There is a reference to more information on the Syntax page, click on the link to read it.

Think of an array as a row of boxes, each containing one item of data.
  • The items of data are related, e.g. they are all scores values.
  • The items are all of the same type: integers, floats, strings, etc. Integers in this case. The type is Dynamic if no type is given.
  • The items can be referred to by the array name plus an index value in square brackets []. The index value is an integer starting from 0.
  • There cannot be gaps in the indexes of the array.

Creating an Array


There are two ways to create the integer array we need.

Using Haxe API


Its syntax is:

var array-name = new Array<Type>();

Here's the way to do it in Haxe:
class Scores
{
 
  public static function main()
  {
    var scores = new Array<Int>();
    scores[0] = 110;
    scores[1] = 170;
    Sys.println( "The first element of scores array is " + scores[0] );
  }
 
}

The output of the program is:
The first element of scores array is 110

What Happened?

  1. The new() function from the Array class creates an array of type integer named scores in line 3. The Array parameter of Int tells Haxe that integers will be stored in the array. Array is a class, so we call its constructor with the new() function and provide the name of the class which is Array<Int>.
  2. The first element in the scores array, that is scores[0], is assigned the value of 110 in line 4. The array name is "scores" and the index of the first element is 0. Note that we are writing to the array.
  3. The next element in the scores array, that is scores[1], is assigned the value of 170 in line 5. The next elements would be scores[2], scores[3] and so on.
  4. The value of scores[0] is added after the string "The first element of scores array is " and printed on the screen in line 6. Note that we are reading from the array.
  5. In a real program, we would use a loop of some kind to read from and write values to an array. See under the "Flow of Control" heading in this wiki.

Array Literal


Here is the second way to create the same array in Haxe. Its syntax is:

var array-name : Array<T> = [element-1, element-2,...];

This is most suited to small arrays where we can populate the array with its values at the same time that we create it. We already know what the values are so we can write them into the program.

class Scores
{
 
  public static function main()
  {
    var scores : Array<Int> = [110,170,870,70,350,240,330];
    neko.Lib.println( "The last element of scores array is " + scores[6] );
  }
 
}

The output of the program is:
The last element of scores array is 330

What Happened?

  1. The integer array named scores is created and the values (110,170, ... ,330) are assigned to the elements of the array in the same order in line 3. We are writing to the array as we create it. The element values are in square brackets [] with each value separated by a comma. All of line 3 is known as an array literal.
  2. The value of scores[6] is added after the string "The last element of scores array is " and printed on the screen in line 4. Note that the 7 elements of the scores array have indexes of 0, 1, 2, 3, 4, 5 and 6 respectively.

The two arrays we have created so far are single-dimensional. They represent one value per array element. What happens if we want to store 2 values per element, e.g. x and y values of screen pixel co-ordinates like 0,0 and 100,100 for lineTo commands in Flash?

Two-dimensional Arrays


Our array would look like this:

Array Element
Data-1
Data-2
0
0
0
1
100
100

Luckily, Haxe can create array to fill this need. For a two-dimensional array, its syntax is:

var array-name : Array<Array<T>> = [
[data,data],
[data,data],
[data,data]
];


The first row of data is for array-name[0][0] and array-name[0][1].
The second row of data is for array-name[1][0] and array-name[1][1].
The third row of data is for array-name[2][0] and array-name[2][1].

Arrays handle the case of related data of one type without having to create a lot of separate variables. It is easier to write programs (and maintain them) if you can keep the number of variables as small as possible.