Hey,
ofDim() is a method in Scala that lets us create multidimensional arrays. Since these let us store data in more than one dimension, we can store data like in a matrix. Let’s take an example.
scala> import Array.ofDim
import Array.ofDim
scala> var a=ofDim[Int](3,3)
a: Array[Array[Int]] = Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0))
scala> var k=1
k: Int = 1
scala> for(i<-0 to 2){
| for(j<-0 to 2){
| a(i)(j)={i+k}
| k+=1
| }
| k-=1
| }
scala> a
The output will be like:
res12: Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6), Array(7, 8, 9))
Hope this helps!
If you need to know more about Scala, join Scala course today and become the expert.
Thanks!!