17.2 Slicing with Range Indexes
Slicing returns a contiguous slice of a one-dimensional array, a contiguous sub-block of a two-dimensional array, and so on. Semantically, it is just a special form of multiple indexing.
Lower and Upper Bound Indexes
For instance, consider supplying an upper and lower bound for an index.
int c; ... int d; d = c[3:6]; // result: d == (c, c, c, c)
The range index
3:6 behaves semantically just like the multiple
(3, 4, 5, 6). In terms of implementation, the sliced
upper and/or lower bounded indices are faster and use less memory
because they do not explicitly create a multiple index, but rather use
a direct loop. They are also easier to read, so should be preferred
over multiple indexes where applicable.
Lower or Upper Bound Indexes
It is also possible to supply just a lower bound, or just an upper
c[3:] is just shorthand for
c[:5] is just shorthand for
Full Range Indexes
Finally, it is possible to write a range index that covers the entire
range of an array, either by including just the range symbol
:) as the index or leaving the index position empty. In both
c[:] are equal to
which in turn is just equal to