Movatterモバイル変換


[0]ホーム

URL:


Scala 3
3.7.4
LearnInstallPlaygroundFind A LibraryCommunityBlog
Scala 3
LearnInstallPlaygroundFind A LibraryCommunityBlog
DocsAPI
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL
Scala 3/scala/Array

Array

scala.Array
See theArray companion object
finalclassArray[T](_length:Int) extendsSerializable,Cloneable

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

val numbers = Array(1, 2, 3, 4)val first = numbers(0) // read the first elementnumbers(3) = 100 // replace the 4th array element with 100val 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 toapply(Int), while line 3 is translated into a call toupdate(Int, T).

Two implicit conversions exist inscala.Predef that are frequently applied to arrays: a conversion toscala.collection.ArrayOps (shown on line 4 of the example above) and a conversion toscala.collection.mutable.ArraySeq (a subtype ofscala.collection.Seq). Both types make available many of the standard operations found in the Scala collections API. The conversion toArrayOps is temporary, as all operations defined onArrayOps return anArray, while the conversion toArraySeq is permanent as all operations return aArraySeq.

The conversion toArrayOps takes priority over the conversion toArraySeq. For instance, consider the following code:

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

ValuearrReversed 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 toArraySeq first and invoking the variant ofreverse that returns anotherArraySeq.

Attributes

See also

Scala Language Specification, for in-depth information on the transformations the Scala compiler makes on Arrays (Sections 6.6 and 6.15 respectively.)

"Scala 2.8 Arrays" the Scala Improvement Document detailing arrays since Scala 2.8.

"The Scala 2.8 Collections' API" section onArray by Martin Odersky for more information.

Companion
object
Source
Array.scala
Graph
Supertypes
traitCloneable
classObject
traitMatchable
classAny

Members list

Value members

Concrete methods

defapply(i:Int):T

The element at given index.

The element at given index.

Indices start at0;xs.apply(0) is the first element of arrayxs. Note the indexing syntaxxs(i) is a shorthand forxs.apply(i).

Value parameters

i

the index

Attributes

Returns

the element at the given index

Throws

ArrayIndexOutOfBoundsExceptionifi < 0 orlength <= i

Source
Array.scala
overridedefclone():Array[T]

Clone the Array.

Clone the Array.

Attributes

Returns

A clone of the Array.

Definition Classes
Source
Array.scala

The length of the array

The length of the array

Attributes

Source
Array.scala
defupdate(i:Int,x:T):Unit

Update the element at given index.

Update the element at given index.

Indices start at0;xs.update(i, x) replaces the ith element in the array. Note the syntaxxs(i) = x is a shorthand forxs.update(i, x).

Value parameters

i

the index

x

the value to be written at indexi

Attributes

Throws

ArrayIndexOutOfBoundsExceptionifi < 0 orlength <= i

Source
Array.scala
In this article
Generated with
Copyright (c) 2002-2025, LAMP/EPFL
Copyright (c) 2002-2025, LAMP/EPFL

[8]ページ先頭

©2009-2025 Movatter.jp