scala

# Array

#### final class Array[T] extends Serializable with 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)`. For more information on these transformations, see the Scala Language Specification v2.8, Sections 6.6 and 6.15 respectively.

Two implicit conversions exist in scala.Predef that are frequently applied to arrays: a conversion to ArrayOps (shown on line 4 of the example above) and a conversion to WrappedArray (a subtype of scala.collections.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`.

Source
  Array.scala
Version

  1.0

See also

  "The Scala 2.8 Collections' API" section on `Array` by Martin Odersky for more information.

Linear Supertypes
Cloneable, Serializable, AnyRef, Any
Ordering

1. Alphabetic
2. By inheritance

Inherited

1. Hide All
2. Show all

1. Array
2. Cloneable
3. Serializable
4. AnyRef
5. Any

Visibility

1. Public
2. All

### Instance Constructors

1. #### new Array(_length: Int)

### Value Members

1. #### final def !=(arg0: AnyRef): Boolean

   Definition Classes
     AnyRef

2. #### final def !=(arg0: Any): Boolean

   Test two objects for inequality.

   Test two objects for inequality.

   returns

     `true` if !(this == that), false otherwise.

   Definition Classes
     Any

3. #### final def ##(): Int

   Equivalent to `x.hashCode` except for boxed numeric types and `null`.

   Equivalent to `x.hashCode` except for boxed numeric types and `null`. For numerics, it returns a hash value which is consistent with value equality: if two value type instances compare as true, then ## will produce the same hash value for each of them. For `null` returns a hashcode where `null.hashCode` throws a `NullPointerException`.

   returns

     a hash value consistent with ==

   Definition Classes
     AnyRef → Any

4. #### final def ==(arg0: AnyRef): Boolean

   Definition Classes
     AnyRef

5. #### final def ==(arg0: Any): Boolean

   Test two objects for equality.

   Test two objects for equality. The expression `x == that` is equivalent to `if (x eq null) that eq null else x.equals(that)`.

   returns

     `true` if the receiver object is equivalent to the argument; `false` otherwise.

   Definition Classes
     Any

6. #### def apply(i: Int): T

   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`

7. #### final def asInstanceOf[T0]: T0

   Cast the receiver object to be of type `T0`.

   Cast the receiver object to be of type `T0`.

   Note that the success of a cast at runtime is modulo Scala's erasure semantics. Therefore the expression `1.asInstanceOf[String]` will throw a `ClassCastException` at runtime, while the expression `List(1).asInstanceOf[List[String]]` will not. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the requested type.

   returns

     the receiver object.

   Definition Classes
     Any
   Exceptions thrown
     ClassCastException

     if the receiver object is not an instance of the erasure of type `T0`.

8. #### def clone(): Array[T]

   Clone the Array.

   Clone the Array.

   returns

     A clone of the Array.

   Definition Classes
     Array → AnyRef

9. #### final def eq(arg0: AnyRef): Boolean

   Tests whether the argument (`arg0`) is a reference to the receiver object (`this`).

   Tests whether the argument (`arg0`) is a reference to the receiver object (`this`).

   The `eq` method implements an equivalence relation on non-null instances of `AnyRef`, and has three additional properties:

   - It is consistent: for any non-null instances `x` and `y` of type `AnyRef`, multiple invocations of `x.eq(y)` consistently returns `true` or consistently returns `false`.
   - For any non-null instance `x` of type `AnyRef`, `x.eq(null)` and `null.eq(x)` returns `false`.
   - `null.eq(null)` returns `true`.

   When overriding the `equals` or `hashCode` methods, it is important to ensure that their behavior is consistent with reference equality. Therefore, if two objects are references to each other (`o1 eq o2`), they should be equal to each other (`o1 == o2`) and they should hash to the same value (`o1.hashCode == o2.hashCode`).

   returns

     `true` if the argument is a reference to the receiver object; `false` otherwise.

   Definition Classes
     AnyRef

10. #### def equals(arg0: Any): Boolean

    The equality method for reference types.

    The equality method for reference types. Default implementation delegates to `eq`.

    See also `equals` in Any.

    returns

      `true` if the receiver object is equivalent to the argument; `false` otherwise.

    Definition Classes
      AnyRef → Any

11. #### def finalize(): Unit

    Called by the garbage collector on the receiver object when there are no more references to the object.

    Called by the garbage collector on the receiver object when there are no more references to the object.

    The details of when and if the `finalize` method is invoked, as well as the interaction between `finalize` and non-local returns and exceptions, are all platform dependent.

    Attributes
      protected[lang]
    Definition Classes
      AnyRef
    Annotations
      @throws()
    Note

      not specified by SLS as a member of AnyRef

12. #### final def getClass(): Class[_]

    A representation that corresponds to the dynamic class of the receiver object.

    A representation that corresponds to the dynamic class of the receiver object.

    The nature of the representation is platform dependent.

    returns

      a representation that corresponds to the dynamic class of the receiver object.

    Definition Classes
      AnyRef → Any
    Note

      not specified by SLS as a member of AnyRef

13. #### def hashCode(): Int

    The hashCode method for reference types.

    The hashCode method for reference types. See hashCode in Any.

    returns

      the hash code value for this object.

    Definition Classes
      AnyRef → Any

14. #### final def isInstanceOf[T0]: Boolean

    Test whether the dynamic type of the receiver object is `T0`.

    Test whether the dynamic type of the receiver object is `T0`.

    Note that the result of the test is modulo Scala's erasure semantics. Therefore the expression `1.isInstanceOf[String]` will return `false`, while the expression `List(1).isInstanceOf[List[String]]` will return `true`. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the specified type.

    returns

      `true` if the receiver object is an instance of erasure of type `T0`; `false` otherwise.

    Definition Classes
      Any

15. #### def length: Int

    The length of the array

16. #### final def ne(arg0: AnyRef): Boolean

    Equivalent to `!(this eq that)`.

    Equivalent to `!(this eq that)`.

    returns

      `true` if the argument is not a reference to the receiver object; `false` otherwise.

    Definition Classes
      AnyRef

17. #### final def notify(): Unit

    Wakes up a single thread that is waiting on the receiver object's monitor.

    Wakes up a single thread that is waiting on the receiver object's monitor.

    Definition Classes
      AnyRef
    Note

      not specified by SLS as a member of AnyRef

18. #### final def notifyAll(): Unit

    Wakes up all threads that are waiting on the receiver object's monitor.

    Wakes up all threads that are waiting on the receiver object's monitor.

    Definition Classes
      AnyRef
    Note

      not specified by SLS as a member of AnyRef

19. #### final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
      AnyRef

20. #### def toString(): String

    Creates a String representation of this object.

    Creates a String representation of this object. The default representation is platform dependent. On the java platform it is the concatenation of the class name, "@", and the object's hashcode in hexadecimal.

    returns

      a String representation of the object.

    Definition Classes
      AnyRef → Any

21. #### def update(i: Int, x: T): Unit

    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`

22. #### final def wait(): Unit

    Definition Classes
      AnyRef
    Annotations
      @throws()

23. #### final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
      AnyRef
    Annotations
      @throws()

24. #### final def wait(arg0: Long): Unit

    Definition Classes
      AnyRef
    Annotations
      @throws()

### Inherited from Cloneable

### Inherited from Serializable

### Inherited from AnyRef

### Inherited from Any