Array Literal

Summary

Creates an array (ordered lists of elements) and, optionally, populates it with elements.

Syntax

[ expression1, expression2, ..., expressionN ]

Parameters

expressionN
Any valid expression.

Description

An array is an ordered list of elements of any type.

Array literals can be used as a shorthand in place of instantiating System.Array. For example, the following statements are equivalent:

1
2
int[] numbers = []; // Using array literals
System.Array<int> numbers = new System.Array<int>(); // Using instantiation

Creating an Array with Pre-populated Elements

Array literals can be used to construct an array with pre-populated elements by specifying the elements during array initialization. The list of elements is enclosed in brackets, with individual elements separated by commas:

1
string[] students = [ "Lisa", "Amber", "Miguel", "Sasha" ];

The elements are each assigned an index, starting with 0 and proceeding sequentially. This is known as a zero-based index. Thus, in the above example, students[2] evaluates to "Miguel" and students[0] evaluates to "Lisa".

Accessing Array Elements

Array elements can be accessed using the array[key] syntax.

1
2
3
4
5
6
7
import System;
 
string[] foo = [ "a", "b", "c" ];
 
Console.log(foo[0]); // "a"
Console.log(foo[1]); // "b"
Console.log(foo[2]); // "c"

Notice how the first element in the array starts at index 0 (zero).

Modifying Existing Array Elements

An individual array element can be modified using the array[key] = ... syntax.

1
2
3
4
5
6
7
8
import System;
 
int[] foo = [ 1, 2, 3 ];
 
Console.log(foo[0]); // 1
foo[0] = 10;
Console.log(foo[0]); // 10
Console.log(foo);    // [ 10, 2, 3 ]

Creating Jagged Arrays

Jagged arrays can be created by nesting arrays inside arrays. Jagged arrays are also known as "arrays of arrays".

1
2
3
4
5
6
7
import System;
 
int[][] numberArrays = [ [ 1, 2 ], [ 3, 4 ] ];
 
Console.log(numberArrays[0]); // [ 1, 2 ]
Console.log(numberArrays[1]); // [ 3, 4 ]
Console.log(numberArrays);    // [ [ 1, 2 ], [ 3, 4 ] ]

Iterating Arrays with 'for' Loops

for loops can be used to iterate over each element of the array:

1
2
3
4
5
6
7
8
9
10
11
12
import System;
 
int[] x = [ 10, 20, 30 ];
 
for (int i = 0; i < x.length; ++i) {
    Console.log(x[i]);
}
 
// Output:
// 10
// 20
// 30

When iterating over arrays, it is common to use the variable names i, j, and so on to represent array indexes. See the JS++ naming conventions.

Accessing All Array Elements with 'foreach'

The foreach loop can be used to iterate over all elements of the array:

1
2
3
4
5
6
7
8
9
10
11
12
import System;
 
int[] x = [ 10, 20, 30 ];
 
foreach(int element in x) {
    Console.log(element);
}
 
// Output:
// 10
// 20
// 30

Unlike for loops, the foreach loop does not provide access to the array index for the current iteration. However, the foreach loop is a shorthand and is easier to type when using the index is not necessary.

Examples

Creating an Empty Array
1
int[] x = [];
Creating a Jagged Array
1
int[][] x = [ [ 1, 2 ], [ 3, 4 ] ];
Using Expressions to Populate an Array
1
2
int x = 2;
int[] evenMultiples = [ 0, x * 2, x * 4, x * 6 ]; // entries are [ 0, 4, 8, 12 ]

See Also

Share

HTML | BBCode | Direct Link