Scala - Array Functions with Examples

1. Introduction

Arrays in Scala are mutable, indexed collections of objects that can store elements of the same type. They offer various functions to perform common operations, making it easier to manage collections of data. This blog post will dive into Scala's array functions, providing examples of how to use each one effectively.

List of Scala Array Functions

Scala provides a plethora of functions to manipulate arrays, such as:

length: Returns the number of elements in the array.

concat: Concatenates two arrays into one.

indexOf: Returns the first index where the given element can be found in the array.

sorted: Returns a new array with elements sorted.

head: Returns the first element of the array.

tail: Returns a new array with all elements except the first.

isEmpty: Returns true if the array is empty.

reverse: Returns a new array with the elements in reversed order.

map: Returns a new array resulting from applying a function to each element in the array.

filter: Returns a new array with all elements that satisfy a condition.

foreach: Applies a procedure to all elements of the array.

2. Program Steps

1. Create sample arrays to demonstrate the functions.

2. Apply various array functions and store or print the results.

3. Execute the code to observe how each function manipulates the array.

3. Code Program

object ArrayFunctionsDemo extends App {
  val array1: Array[Int] = Array(1, 2, 3, 4, 5)
  val array2: Array[Int] = Array(6, 7, 8, 9, 10)

  // Length of array
  println(s"Length of array1: ${array1.length}")

  // Concatenate two arrays
  val concatenated: Array[Int] = Array.concat(array1, array2)
  println(s"Concatenated array: ${concatenated.mkString(", ")}")

  // Index of an element
  println(s"Index of '4' in array1: ${array1.indexOf(4)}")

  // Sort an array
  val unsortedArray: Array[Int] = Array(4, 3, 1, 5, 2)
  val sortedArray: Array[Int] = unsortedArray.sorted
  println(s"Sorted array: ${sortedArray.mkString(", ")}")

  // Head of the array
  println(s"Head of array1: ${array1.head}")

  // Tail of the array
  println(s"Tail of array1: ${array1.tail.mkString(", ")}")

  // Check if array is empty
  println(s"Is array1 empty? ${array1.isEmpty}")

  // Reverse an array
  println(s"Reverse of array1: ${array1.reverse.mkString(", ")}")

  // Map function to square the elements
  val squaredArray: Array[Int] = => x * x)
  println(s"Squared array1: ${squaredArray.mkString(", ")}")

  // Filter even numbers
  val evenNumbers: Array[Int] = array1.filter(_ % 2 == 0)
  println(s"Even numbers in array1: ${evenNumbers.mkString(", ")}")

  // Foreach to print elements
  print("Elements in array1: ")


Length of array1: 5
Concatenated array: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Index of '4' in array1: 3
Sorted array: 1, 2, 3, 4, 5
Head of array1: 1
Tail of array1: 2, 3, 4, 5
Is array1 empty? false
Reverse of array1: 5, 4, 3, 2, 1
Squared array1: 1, 4, 9, 16, 25
Even numbers in array1: 2, 4
Elements in array1: 12345


1. array1.length retrieves the size of array1, which is 5.

2. Array.concat(array1, array2) joins array1 and array2 into a new array called concatenated.

3. array1.indexOf(4) finds the first index of the element 4 in array1.

4. unsortedArray.sorted sorts the elements of unsortedArray in ascending order to create sortedArray.

5. array1.head returns the first element in array1, which is 1.

6. array1.tail returns a new array containing all elements of array1 except the first.

7. array1.isEmpty checks if array1 is empty, which it is not.

8. array1.reverse reverses the order of elements in array1.

9. => x * x) applies a function to square each element in array1, resulting in squaredArray.

10. array1.filter(_ % 2 == 0) filters array1 to include only even numbers, yielding evenNumbers.

11. array1.foreach(print) iterates over array1 and prints each element. Note that foreach returns Unit and is used for side effects.