Operators: Array
Use 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.
int[] x = new int[] {1, 2, 3};
- declare
int[] x
; allocate1-d int array
instance withlength [3]
β1-d int array reference
; storeint 1
toindex [0]
of1-d int array reference
; storeint 2
toindex [1]
of1-d int array reference
; storeint 3
toindex [2]
of1-d int array reference
; store1-d int array reference
tox
;
- declare
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 1
toi
- declare
long l
; storelong 2
tol
- declare
float f
; storefloat 3.0
tof
- declare
double d
; storedouble 4.0
tod
- declare
String s
; storeString "5"
tos
- declare
def array
; allocate1-d def array
instance withlength [4]
β1-d def array reference
; load fromi
βint 1
; implicit castint 1
todef
βdef
; storedef
toindex [0]
of1-d def array reference
; load froml
βlong 2
; implicit castlong 2
todef
βdef
; storedef
toindex [1]
of1-d def array reference
; load fromf
βfloat 3.0
; load fromd
βdouble 4.0
; promotefloat 3.0
anddouble 4.0
: resultdouble
; implicit castfloat 3.0
todouble 3.0
βdouble 3.0
; multiplydouble 3.0
anddouble 4.0
βdouble 12.0
; implicit castdouble 12.0
todef
βdef
; storedef
toindex [2]
of1-d def array reference
; load froms
βString "5"
; implicit castString "5"
todef
βdef
; storedef
toindex [3]
of1-d def array reference
; implicit cast1-d int array reference
todef
βdef
; storedef
toarray
- declare
Use 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
int
type value or a value that is castable to anint
type 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.
int[] x = new int[2]; x[0] = 2; x[1] = 5; int y = x[0] + x[1]; int z = 1; int i = x[z];
- declare
int[] x
; allocate1-d int array
instance withlength [2]
β1-d int array reference
; store1-d int array reference
tox
- load from
x
β1-d int array reference
; storeint 2
toindex [0]
of1-d int array reference
; - load from
x
β1-d int array reference
; storeint 5
toindex [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 2
andint 5
βint 7
; storeint 7
toy
- declare
int z
; storeint 1
toz
; - 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 5
toi
;
- declare
Array access with the
def
type.def d = new int[2]; d[0] = 2; d[1] = 5; def x = d[0] + d[1]; def y = 1; def z = d[y];
- declare
def d
; allocate1-d int array
instance withlength [2]
β1-d int array reference
; implicit cast1-d int array reference
todef
βdef
; storedef
tod
- load from
d
βdef
implicit castdef
to1-d int array reference
β1-d int array reference
; storeint 2
toindex [0]
of1-d int array reference
; - load from
d
βdef
implicit castdef
to1-d int array reference
β1-d int array reference
; storeint 5
toindex [1]
of1-d int array reference
; - declare
int x
; load fromd
βdef
implicit castdef
to1-d int array reference
β1-d int array reference
; load fromindex [0]
of1-d int array reference
βint 2
; load fromd
βdef
implicit castdef
to1-d int array reference
β1-d int array reference
; load fromindex [1]
of1-d int array reference
βint 5
; addint 2
andint 5
βint 7
; implicit castint 7
todef
βdef
; storedef
tox
- declare
def y
; implicit castint 1
todef
βdef
; storedef
toy
; - declare
int i
; load fromd
βdef
implicit castdef
to1-d int array reference
β1-d int array reference
; load fromy
βdef
; implicit castdef
toint 1
βint 1
; load fromindex [1]
of1-d int array reference
βint 5
; implicit castint 5
todef
; storedef
toz
;
- declare
Array access with a multi-dimensional array.
int[][][] ia3 = new int[2][3][4]; ia3[1][2][3] = 99; int i = ia3[1][2][3];
- declare
int[][][] ia
; allocate3-d int array
instance with length[2, 3, 4]
β3-d int array reference
; store3-d int array reference
toia3
- load from
ia3
β3-d int array reference
; storeint 99
toindex [1, 2, 3]
of3-d int array reference
- declare
int i
; load fromia3
β3-d int array reference
; load fromindex [1, 2, 3]
of3-d int array reference
βint 99
; storeint 99
toi
- declare
An 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
length
field.int[] x = new int[10]; int l = x.length;
- declare
int[] x
; allocate1-d int array
instance withlength [2]
β1-d int array reference
; store1-d int array reference
tox
- declare
int l
; loadx
β1-d int array reference
; loadlength
from1-d int array reference
βint 10
; storeint 10
tol
;
- declare
Use 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
int
type value or a value that is castable to anint
type value is specified for a dimensionβs size.
Grammar
new_array: 'new' TYPE ('[' expression ']')+;
Examples
Allocation of different array types.
int[] x = new int[5]; x = new int[10]; int y = 2; def z = new def[y][y*2];
- declare
int[] x
; allocate1-d int array
instance withlength [5]
β1-d int array reference
; store1-d int array reference
tox
- allocate
1-d int array
instance withlength [10]
β1-d int array reference
; store1-d int array reference
tox
- declare
int y
; storeint 2
toy
; - declare
def z
; load fromy
βint 2 @0
; load fromy
βint 2 @1
; multiplyint 2 @1
byint 2 @2
βint 4
; allocate2-d int array
instance with length[2, 4]
β2-d int array reference
; implicit cast2-d int array reference
todef
βdef
; storedef
toz
;
- declare