Site icon DataFlair

Scala Array and Multidimensional Arrays in Scala

Scala Arrays and Multidimensional Arrays in Scala

Scala Arrays 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)
    | }

1
2
3

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.

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.

Reference

Exit mobile version