Scala Array and Multidimensional Arrays in Scala

Free Scala course with real-time projects Start Now!!

Today, we will learn about Scala arrays, how to declare and process them, and multidimensional arrays. We will also see how to create them with Range and concatenating them.

So, let’s begin our tutorial on Scala Array.

What is Scala Array?

When we want to hold a number of elements of the same kind in a collection, we use an array. An array is sequential and is of a fixed size.

How to Declare Scala Array?

We can declare Scala arrays in two ways.

a. Adding Elements Later

We can create an array in Scala with default initial elements according to data type, and then fill in values later.

scala> var a=new Array[Int](3) //This can hold three elements
a: Array[Int] = Array(0, 0, 0)
scala> a(1)
res6: Int = 0
scala> a(1)=2 //Assigning second element
scala> a
res8: Array[Int] = Array(0, 2, 0)

We can also mention the type of Scala array when declaring it:

scala> var a:Array[Int]=new Array[Int](3)
a: Array[Int] = Array(0, 0, 0)
scala> a(4/2)=3
scala> a
res10: Array[Int] = Array(0, 0, 3)

b. Define Scala Array with Values

We can also define a Scala array specifying its values in place.

scala> var a=Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)
scala> a(4)
java.lang.ArrayIndexOutOfBoundsException: 4
 ... 28 elided
scala> a(0)
res12: Int = 1

How to Process an Array in Scala?

Since we know the type of elements and the size of the Scala array, we can use loop control structures to process an array. Let’s take an example of processing Scala Array.

To iterate over the array:

scala> var a=Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)
scala> for(i<-a){
    | println(i)
    | }


To calculate the sum of all the elements:

scala> var sum=0.0
sum: Double = 0.0
scala> for(i<-a){
    | sum+=i}
scala> sum
res2: Double = 6.0
Finding the highest value from the array:
scala> var max=a(0)
max: Int = 1
scala> for(i<-a){
    | if(i>max){
    | max=i}
    | }
scala> max
res4: Int = 3

Concatenating Arrays in Scala

We can append a Scala array to another using the concat() method. This takes the arrays as parameters- in order.

scala> var a=Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)
scala> var b=Array(4,5,6)
b: Array[Int] = Array(4, 5, 6)

We’ll need to import the Array._ package

scala> import Array._
import Array._

Now, let’s call concat().

scala> var c=concat(a,b)
c: Array[Int] = Array(1, 2, 3, 4, 5, 6)

Creating a Scala Array with Range

The range() method will give us integers from the start to the end. We can also specify a step to denote the interval.

We can use this to create an array to iterate on.

scala> var a=range(2,15)
a: Array[Int] = Array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
scala> var b=range(15,2,-2)
b: Array[Int] = Array(15, 13, 11, 9, 7, 5, 3)

Scala Multidimensional Arrays

Why settle in two dimensions when we can have multiple? Sometimes, we may need more than two.

scala> var a=ofDim[Int](3,3)
a: Array[Array[Int]] = Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0))
Now, let’s fill in some values.
scala> for(i<-0 to 2){
    | for(j<-0 to 2){
    | a(i)(j)={i+j}
    | }
    | }
scala> a
res10: Array[Array[Int]] = Array(Array(0, 1, 2), Array(1, 2, 3), Array(2, 3, 4))

So, this was all about Scala Array Tutorial. Hope you like our explanation.


With arrays in Scala, we can deal with a set of values of the same kind. Feel free to ask us any doubts in the comment section.


Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

4 Responses

  1. Krish Se says:

    In Scala, How to traverse Array without java.lang.ArrayIndexOutOfBoundsException: 4. I tried to get next and previous element, Is there any better way to get this done?
    def main(args: Array[String]) {
    var myList = Array(1,5,10,30,60,90)
    // println(getNextItem(myList,1))
    def getItem(array: Array[Int], index: Int): Int = {
    if (array.isEmpty) -1
    if (array.length < 0 || array.length <= index) -1
    else array(index)
    def getNextItem(array: Array[Int], index: Int): Int = {
    def getPrevioustItem(array: Array[Int], index: Int): Int = {

  2. Ansuman Satpathy says:

    What is the ofDim keyword?

    • DataFlair Team says:

      Hey there, Ansuman

      Thanks for commenting on Scala array tutorial. Here, is your answer…

      ofDim is a method with Scala for the ability to create multidimensional arrays. Think of it as an abbreviation for ‘of Dimension’.
      So if you do var a=pfDim[Int](3,3), you get an array of dimensions 3×3.

      It has the following syntaxes:

      def ofDim[T](n1: Int, n2: Int, n3: Int, n4: Int, n5: Int)(implicit arg0: ClassTag[T]): Array[Array[Array[Array[Array[T]]]]]
      def ofDim[T](n1: Int, n2: Int, n3: Int, n4: Int)(implicit arg0: ClassTag[T]): Array[Array[Array[Array[T]]]] //4-dimensional array
      def ofDim[T](n1: Int, n2: Int, n3: Int)(implicit arg0: ClassTag[T]): Array[Array[Array[T]]]
      def ofDim[T](n1: Int, n2: Int)(implicit arg0: ClassTag[T]): Array[Array[T]] //2-dimensional array
      def ofDim[T](n1: Int)(implicit arg0: ClassTag[T]): Array[T]

      Keep learning and Keep visiting Data Flair

  3. sreepal says:

    scala> var a=Array(1,2,3)
    a: Array[Int] = Array(1, 2, 3)
    scala> var max=a(0)
    max: Int = 1
    scala> for(imax){
    | max=i}
    | }
    scala> max
    res4: Int = 3

    in the above code at the end how the max value is 3, can you please explain me.

Leave a Reply

Your email address will not be published. Required fields are marked *