The element at given index.
The element at given index.
Indices start at 0
; xs.apply(0)
is the first element of array xs
.
Note the indexing syntax xs(i)
is a shorthand for xs.apply(i)
.
the index
the element at the given index
ArrayIndexOutOfBoundsException
if i < 0
or length <= i
Clone the Array.
The length of the array
Update the element at given index.
Update the element at given index.
Indices start at 0
; xs.update(i, x)
replaces the ith element in the array.
Note the syntax xs(i) = x
is a shorthand for xs.update(i, x)
.
the index
the value to be written at index i
ArrayIndexOutOfBoundsException
if i < 0
or length <= i
Arrays are mutable, indexed collections of values.
Array[T]
is Scala's representation for Java'sT[]
.Arrays make use of two common pieces of Scala syntactic sugar, shown on lines 2 and 3 of the above example code. Line 2 is translated into a call to
apply(Int)
, while line 3 is translated into a call toupdate(Int, T)
.Two implicit conversions exist in scala.Predef that are frequently applied to arrays: a conversion to scala.collection.mutable.ArrayOps (shown on line 4 of the example above) and a conversion to scala.collection.mutable.WrappedArray (a subtype of scala.collection.Seq). Both types make available many of the standard operations found in the Scala collections API. The conversion to
ArrayOps
is temporary, as all operations defined onArrayOps
return anArray
, while the conversion toWrappedArray
is permanent as all operations return aWrappedArray
.The conversion to
ArrayOps
takes priority over the conversion toWrappedArray
. For instance, consider the following code:Value
arrReversed
will be of typeArray[Int]
, with an implicit conversion toArrayOps
occurring to perform thereverse
operation. The value ofseqReversed
, on the other hand, will be computed by converting toWrappedArray
first and invoking the variant ofreverse
that returns anotherWrappedArray
.1.0
"The Scala 2.8 Collections' API" section on
Array
by Martin Odersky for more information."Scala 2.8 Arrays" the Scala Improvement Document detailing arrays since Scala 2.8.
Scala Language Specification, for in-depth information on the transformations the Scala compiler makes on Arrays (Sections 6.6 and 6.15 respectively.)