As with all other data in JScript, variables can store arrays. Type annotation can specify that a variable must contain an array object or a typed array, but it does not provide an initial array. To store an array in a variable, you must declare an array and assign it to that variable.
Declaring a JScript array object creates a new Array object, while declaring a typed array reserves a section of memory large enough to store every element of the array. Both types of arrays can be declared either by using the new operator to explicitly construct a new array, or by using an array literal.
Array Declaration with the new Operator
To declare a new JScript Array object, you can use the new operator with the Array constructor. Since you can dynamically add members to a JScript array, you do not need to specify an initial size for the array. In this example, a1
is assigned a zero-length array.
В | Copy Code |
---|---|
var a1 = new Array(); |
To assign an initial length to the array created with the Array constructor, pass an integer to the array constructor. The array length must be zero or positive. The following code assigns an array of length 10 to a2
.
В | Copy Code |
---|---|
var a2 = new Array(10); |
If more than one parameter or a single nonnumeric parameter is passed to the Array constructor, the resulting array contains all the parameters as the array elements. For example, the following code creates an array in which element 0 is the number 10, element 1 is the string "Hello", and element 2 is the current date.
В | Copy Code |
---|---|
var a3 = new Array(10, "Hello", Date()); |
The new operator can also declare typed arrays. Since typed arrays cannot accept dynamically added elements, the declaration must specify the size of the array. The constructor for typed arrays uses square brackets instead of parentheses around the array size. For example, the following code declares an array of five integers.
В | Copy Code |
---|---|
var i1 : int[] = new int[5]; |
The new operator can also declare multidimensional arrays. The following example declares a three-by-four-by-five array of integers.
В | Copy Code |
---|---|
var i2 : int[,,] = new int[3,4,5]; |
When declaring an array of arrays, the base array must be declared before declaring the subarrays; they cannot all be declared at the same time. This provides flexibility in determining the sizes of the subarrays. In this example, the first subarray has length 1, the second has length 2, and so on.
В | Copy Code |
---|---|
// First, declare a typed array of type int[], and initialize it. var i3 : int[][] = new (int[])[4]; // Second, initialize the subarrays. for(var i=0; i<4; i++) i3[i] = new int[i+1]; |
Array Declaration with Array Literals
An alternative way to simultaneously declare and initialize arrays is to use array literals. An array literal represents a JScript Array. Since JScript arrays interoperate with typed arrays, however, literals can also be used to initialize typed arrays. For more information, see Array Data.
Array literals can easily initialize one-dimensional arrays. Note that the compiler will attempt to convert the data from the array literal to the correct type when assigned to a typed array. In this example, a literal array is assigned to a JScript array and a typed array.
В | Copy Code |
---|---|
var al1 : Array = [1,2,"3"]; var il1 : int[] = [1,2,"3"]; |
Array literals can also initialize arrays of arrays. In the following example, an array of two arrays of integers initializes both the JScript array and the typed array.
В | Copy Code |
---|---|
var al1 : Array = [[1,2,3],[4,5,6]]; var il1 : int[][] = [[1,2,3],[4,5,6]]; |
Array literals cannot initialize multidimensional typed arrays.