UIEvolutin Inc.
UJML Language Reference
Arrays Explained

A simplified explanation of arrays.

Arrays are used to store a series of values in a single variable name. Each of these values is an 'element' of the array and is associated with a number, called an 'index.' You refer to an element of an array using the variable name followed by the index contained in square brackets. These index values are zero-based, meaning that the first element is zero, the second element is one, and so on. For example, the scripting statement 'a[3]=128;' assigns the constant value '128' to the fourth element of array 'a.' Using zero as the starting number when indexing might seem arbitrary and overly complex but there are many good computer-science reasons for doing it this way; so most computer languages use zero-based indexing. And it isn't really that difficult to learn! 

Let us suppose that you have created a one-dimensional array of string values with five elements and you have set each element of the array to the string value of its position in the array, not its index. Laid out on a grid, the result would look something like this:

0 
"1" 
1 
"2" 
2 
"3" 
3 
"4" 
4 
"5" 

If the above was an array variable called 'a,' this means that the value of 'a[2]' is "3." 

 

Of course you can use any values you like in the array. The code to set this array looks like this:

a[0] = "Red";
a[1] = "Green";
a[2] = "Blue";
a[3] = "Yellow";
a[4] = "White";

 

Which, laid out in a grid, looks like this:

0 
"Red
1 
"Green
2 
"Blue
3 
"Yellow
4 
"White

 

Because UJML supports arrays with one or more dimensions you must provide an index for each dimension of an array. For example, the scripting statement 'a[3][2] = 128;' assigns the literal value '128' to the element located at the fourth index of the first dimension and the third index of the second dimension of array 'a.

So if you created a grid similar to the above, but for a two-dimensional array of five elements by four elements, you could lay it out like this:

 
0 
1 
2 
3 
0 
"1, 1" 
"1, 2" 
"1, 3" 
"1, 4" 
1 
"2, 1" 
"2, 2" 
"2, 3" 
"2, 4" 
2 
"3, 1" 
"3, 2" 
"3, 3" 
"3, 4" 
3 
"4, 1" 
"4, 2" 
"4, 3" 
"4, 4" 
4 
"5, 1" 
"5, 2" 
"5, 3" 
"5, 4" 

If the above was an array variable called 'a', the value of 'a[2][3]' is "3, 4." 

Each dimension in an array has a maximum number of indices, which are specified when the array variable is declared. So the valid index value for each array dimension ranges from zero to the index count for that dimension, minus one. This means that, if you declare array 'a' with two dimensions of five elements by four elements, the scripting statement 'a[5][4] = 128;' would fail with an array out of bounds error. You can declare your array with one or more dimensions using the size attribute of the var tag used for your array variable. See Variables

Arrays are most commonly used to group similar data, such as the names of the months, or to allow iteration over the elements in an array in a scripting statement. For detailed examples of using arrays in UJML, see Arrays.

Copyright (c) 2000-2007 UIEvolution, Inc. All rights reserved.
What do you think about this topic? Send feedback!