Scala Array Basics: Declaration, Operations, and Methods
This article introduces Scala arrays, covering their declaration, element access, multidimensional arrays, common operations such as iteration, summation, finding maximum values, concatenation, range creation, and a comprehensive list of array methods, all illustrated with clear code examples.
Scala provides fixed-size, same-type element arrays, a fundamental data structure in many programming languages.
Array Declaration
Arrays are declared by specifying the type and length, e.g., var z:Array[String] = new Array[String](3) or var z = new Array[String](3). Elements are accessed via zero‑based indices.
Values can be assigned using z(0) = "Runoob", z(1) = "Baidu", and expressions such as z(4/2) = "Google" (equivalent to z(2) = "Google").
An alternative literal syntax is var z = Array("Runoob", "Baidu", "Google").
Processing Arrays
Typical operations use basic for loops. Example creates an array, prints elements, computes total sum, and finds the maximum value.
object Test {
def main(args: Array[String]) {
var myList = Array(1.9, 2.9, 3.4, 3.5)
// print elements
for (x <- myList) println(x)
// sum
var total = 0.0
for (i <- 0 to (myList.length - 1)) total += myList(i)
println("Total = " + total)
// max
var max = myList(0)
for (i <- 1 to (myList.length - 1))
if (myList(i) > max) max = myList(i)
println("Max = " + max)
}
}Running the program outputs each element, the sum (11.7), and the maximum (3.5).
Multidimensional Arrays
Arrays can contain other arrays, forming matrices. A two‑dimensional array can be created with ofDim[Int](3,3). var myMatrix = ofDim[Int](3,3) Example fills a 3×3 matrix with column indices and prints it.
import Array._
object Test {
def main(args: Array[String]) {
var myMatrix = ofDim[Int](3,3)
for (i <- 0 to 2; j <- 0 to 2) myMatrix(i)(j) = j
for (i <- 0 to 2) {
for (j <- 0 to 2) print(" " + myMatrix(i)(j))
println()
}
}
}Array Concatenation
The concat() method merges multiple arrays.
import Array._
object Test {
def main(args: Array[String]) {
var myList1 = Array(1.9, 2.9, 3.4, 3.5)
var myList2 = Array(8.9, 7.9, 0.4, 1.5)
var myList3 = concat(myList1, myList2)
for (x <- myList3) println(x)
}
}Range Arrays
The range() method creates arrays over a numeric interval, with an optional step.
var myList1 = range(10, 20, 2)
var myList2 = range(10,20)Printing these yields “10 12 14 16 18” and “10 11 12 13 14 15 16 17 18 19”.
Scala Array Methods
Common array operations require import Array._. Key methods include apply, concat, copy, empty, iterate, fill, ofDim, range, and tabulate. The table below summarizes their signatures and descriptions.
No.
Method and Description
1 def apply( x: T, xs: T* ): Array[T] – creates an array of type T.
2 def concat[T]( xss: Array[T]* ): Array[T] – merges arrays.
3
def copy( src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int ): Unit– copies elements between arrays.
4 def empty[T]: Array[T] – returns an empty array.
5
def iterate[T]( start: T, len: Int )( f: (T) => T ): Array[T]– creates an array by repeatedly applying a function.
6 def fill[T]( n: Int )(elem: => T): Array[T] – creates an array of length n filled with elem.
7
def fill[T]( n1: Int, n2: Int )( elem: => T ): Array[Array[T]]– creates a two‑dimensional array.
8 def ofDim[T]( n1: Int ): Array[T] – creates a one‑dimensional array of length n1.
9 def ofDim[T]( n1: Int, n2: Int ): Array[Array[T]] – creates a two‑dimensional array.
10
def ofDim[T]( n1: Int, n2: Int, n3: Int ): Array[Array[Array[T]]]– creates a three‑dimensional array.
11 def range( start: Int, end: Int, step: Int ): Array[Int] – creates an array over a range with step.
12 def range( start: Int, end: Int ): Array[Int] – creates an array over a range.
13 def tabulate[T]( n: Int )(f: (Int)=> T): Array[T] – creates an array by applying a function to each index.
14
def tabulate[T]( n1: Int, n2: Int )( f: (Int, Int ) => T): Array[Array[T]]– creates a two‑dimensional array by applying a function to each coordinate.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Big Data Technology & Architecture
Wang Zhiwu, a big data expert, dedicated to sharing big data technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
