Scala Arrays and Multidimensional Arrays in Scala 2

1. Scala Arrays and Multidimensional Arrays

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 Arrays in Scala.

Scala Arrays and Multidimensional Arrays

Scala Arrays and Multidimensional Arrays

2. Introduction to Arrays in Scala

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.

3. Declaring a Scala Array

In Scala, we can declare 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 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. Defining an 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

4. Processing an Array

Since we know the type of elements and the size of the 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

5. Concatenating Arrays

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)

Any Doubt yet in Scala Arrays? Please Comment.

6. Creating an 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. Check Range() in Python for reference.

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)

7. Scala Multidimensional Arrays

Why settle at 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))

8. Conclusion

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.

2 thoughts on “Scala Arrays and Multidimensional Arrays in Scala

  • Krish Se

    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 = {

Comments are closed.