Timber | Implementation | Spar | Overview | foreach | arrays | array expressions | inline | Vectors | Templates | array interface | elastic array interface | example | Sins |
a
, and then construct a copy of the
first row of a
, and assign it to v
:
int[*,*] a = {{0,1,2},{3,4,5},{6,7,8}}; int[*] v = a[0,0:a.getSize(1)]; |
Note that v
is a copy, so subsequent assignments to elements
of v
are not visible in a
. Contrary to array range notations
in many other languages, the top of the specified range is the first element
not to be included.
The usual range shorthands apply: if no start of the range is given,
0 is assumed, and if no end of the range is given, the size in that
dimension is assumed. Thus the declaration of v
in the previous
code fragment could be written as:
int[*] v = a[0,:];
foreach
statement that is executed for all elements of a selected range. For
example, the code fragment
Block[*,*] a = new Block[5,7]; a[:,:].init();will invoke the method
init
on all elements of Block
array a
. Since this is equivalent to a erm{foreach} statement, the
init
method of each of the array elements is not invoked in a
prescribed order.
Similarly, array assignments are a shorthand for repeated assignments. For example:
int[*,*] a = new int[5,7];will zero the entire arraya[:,:] = 0;
a
.
The expression at the right-hand side of the assignment will be evaluated only once. Thus,
int ix = 0; int[*,*] a = new int[5,7];will again zero the entire arraya[:,:] = ix++;
a
, and will leave ix
with
the value 1.
Last but not least, array assignments may contain an array at the
right-hand side, instead of a single element. In that case every iteration
of the foreach
will use the implicit iteration vector as index
for every assignment.
For example:
int[*,*] a = new int[5,7]; int[*,*] b = new int a.getSize();will copyb[:,:] = 1; a[:,:] = b;
b
into a
. The last statement could also be written
as:
a[:,:] = b[:,:];but a naive compiler would first create a copy of
b
, and leave it
for the garbage collector.
Array statements never change the size of the array they work on. Any
access that is out of bounds is detected at compile-time or run-time,
and causes an error message or an IndexOutOfBoundsException
exception.
Last modified Sat May 13 15:50:56 2017 UT by Kees van Reeuwijk.