Class/Object

scala

Array

Related Docs: object Array | package scala

Permalink

final class Array[T] extends java.io.Serializable with java.lang.Cloneable

Arrays are mutable, indexed collections of values. Array[T] is Scala's representation for Java's T[].

val numbers = Array(1, 2, 3, 4)
val first = numbers(0) // read the first element
numbers(3) = 100 // replace the 4th array element with 100
val biggerNumbers = numbers.map(_ * 2) // multiply all numbers by two

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 to update(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 on ArrayOps return an Array, while the conversion to WrappedArray is permanent as all operations return a WrappedArray.

The conversion to ArrayOps takes priority over the conversion to WrappedArray. For instance, consider the following code:

val arr = Array(1, 2, 3)
val arrReversed = arr.reverse
val seqReversed : Seq[Int] = arr.reverse

Value arrReversed will be of type Array[Int], with an implicit conversion to ArrayOps occurring to perform the reverse operation. The value of seqReversed, on the other hand, will be computed by converting to WrappedArray first and invoking the variant of reverse that returns another WrappedArray.

Version

1.0

See also

"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.)

Linear Supertypes
java.lang.Cloneable, java.io.Serializable, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Array
  2. Cloneable
  3. Serializable
  4. AnyRef
  5. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Array(_length: Int)

    Permalink

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. def apply(i: Int): T

    Permalink

    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).

    i

    the index

    returns

    the element at the given index

    Exceptions thrown

    ArrayIndexOutOfBoundsException if i < 0 or length <= i

  5. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  6. def clone(): Array[T]

    Permalink

    Clone the Array.

    Clone the Array.

    returns

    A clone of the Array.

    Definition Classes
    Array → AnyRef
  7. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  8. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  9. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  10. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  11. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  12. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  13. def length: Int

    Permalink

    The length of the array

  14. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  15. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  16. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  17. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  18. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  19. def update(i: Int, x: T): Unit

    Permalink

    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).

    i

    the index

    x

    the value to be written at index i

    Exceptions thrown

    ArrayIndexOutOfBoundsException if i < 0 or length <= i

  20. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  22. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from java.lang.Cloneable

Inherited from java.io.Serializable

Inherited from AnyRef

Inherited from Any

Ungrouped