Operators: Array
editOperators: Array
editArray Initialization
editUse the array initialization operator '[] {}' to allocate a single-dimensional
array type instance to the heap with a set of pre-defined
elements. Each value used to initialize an element in the array type instance is
cast to the specified element type value upon insertion. The order of specified
values is maintained.
Errors
- If a value is not castable to the specified type value.
Grammar
array_initialization: 'new' TYPE '[' ']' '{' expression_list '}'
| 'new' TYPE '[' ']' '{' '}';
expression_list: expression (',' expression);
Example:
-
Array initialization with static values.
-
Array initialization with non-static values.
int i = 1; long l = 2L; float f = 3.0F; double d = 4.0; String s = "5"; def array = new def[] {i, l, f*d, s};
declare
int i; storeint 1toideclare
long l; storelong 2toldeclare
float f; storefloat 3.0tofdeclare
double d; storedouble 4.0toddeclare
String s; storeString "5"tosdeclare
def array; allocate1-d def arrayinstance withlength [4]→1-d def array reference; load fromi→int 1; implicit castint 1todef→def; storedeftoindex [0]of1-d def array reference; load froml→long 2; implicit castlong 2todef→def; storedeftoindex [1]of1-d def array reference; load fromf→float 3.0; load fromd→double 4.0; promotefloat 3.0anddouble 4.0: resultdouble; implicit castfloat 3.0todouble 3.0→double 3.0; multiplydouble 3.0anddouble 4.0→double 12.0; implicit castdouble 12.0todef→def; storedeftoindex [2]of1-d def array reference; load froms→String "5"; implicit castString "5"todef→def; storedeftoindex [3]of1-d def array reference; implicit cast1-d int array referencetodef→def; storedeftoarray
Array Access
editUse the array access operator '[]' to store a value to or load a value from
an array type value. Each element of an array type value is
accessed with an int type value to specify the index to store/load. The range
of elements within an array that are accessible is [0, size) where size is the
number of elements specified at the time of allocation. Use a negative int
type value as an index to access an element in reverse from the end of an array
type value within a range of [-size, -1].
Errors
-
If a value other than an
inttype value or a value that is castable to aninttype value is provided as an index. - If an element is accessed outside of the valid ranges.
Grammar
brace_access: '[' expression ']'
Examples
-
Array access with a single-dimensional array.
declare
int[] x; allocate1-d int arrayinstance withlength [2]→1-d int array reference; store1-d int array referencetoxload from
x→1-d int array reference; storeint 2toindex [0]of1-d int array reference;load from
x→1-d int array reference; storeint 5toindex [1]of1-d int array reference;declare
int y; load fromx→1-d int array reference; load fromindex [0]of1-d int array reference→int 2; load fromx→1-d int array reference; load fromindex [1]of1-d int array reference→int 5; addint 2andint 5→int 7; storeint 7toydeclare
int z; storeint 1toz;declare
int i; load fromx→1-d int array reference; load fromz→int 1; load fromindex [1]of1-d int array reference→int 5; storeint 5toi; -
Array access with the
deftype.declare
def d; allocate1-d int arrayinstance withlength [2]→1-d int array reference; implicit cast1-d int array referencetodef→def; storedeftodload from
d→defimplicit castdefto1-d int array reference→1-d int array reference; storeint 2toindex [0]of1-d int array reference;load from
d→defimplicit castdefto1-d int array reference→1-d int array reference; storeint 5toindex [1]of1-d int array reference;declare
int x; load fromd→defimplicit castdefto1-d int array reference→1-d int array reference; load fromindex [0]of1-d int array reference→int 2; load fromd→defimplicit castdefto1-d int array reference→1-d int array reference; load fromindex [1]of1-d int array reference→int 5; addint 2andint 5→int 7; implicit castint 7todef→def; storedeftoxdeclare
def y; implicit castint 1todef→def; storedeftoy;declare
int i; load fromd→defimplicit castdefto1-d int array reference→1-d int array reference; load fromy→def; implicit castdeftoint 1→int 1; load fromindex [1]of1-d int array reference→int 5; implicit castint 5todef; storedeftoz; -
Array access with a multi-dimensional array.
declare
int[][][] ia; allocate3-d int arrayinstance with length[2, 3, 4]→3-d int array reference; store3-d int array referencetoia3load from
ia3→3-d int array reference; storeint 99toindex [1, 2, 3]of3-d int array referencedeclare
int i; load fromia3→3-d int array reference; load fromindex [1, 2, 3]of3-d int array reference→int 99; storeint 99toi
Array Length
editAn array type value contains a read-only member field named length. The
length field stores the size of the array as an int type value where size is
the number of elements specified at the time of allocation. Use the
field access operator to load the field length
from an array type value.
Examples
-
Access the
lengthfield.
New Array
editUse the new array operator 'new []' to allocate an array type instance to
the heap. Specify the element type following the new token. Specify each
dimension with the [ and ] tokens following the element type name. The size
of each dimension is specified by an int type value in between each set of [
and ] tokens.
Errors
-
If a value other than an
inttype value or a value that is castable to aninttype value is specified for a dimension’s size.
Grammar
new_array: 'new' TYPE ('[' expression ']')+;
Examples
-
Allocation of different array types.
declare
int[] x; allocate1-d int arrayinstance withlength [5]→1-d int array reference; store1-d int array referencetoxallocate
1-d int arrayinstance withlength [10]→1-d int array reference; store1-d int array referencetoxdeclare
int y; storeint 2toy;declare
def z; load fromy→int 2 @0; load fromy→int 2 @1; multiplyint 2 @1byint 2 @2→int 4; allocate2-d int arrayinstance with length[2, 4]→2-d int array reference; implicit cast2-d int array referencetodef→def; storedeftoz;