Scala – Array

  • date 6th October, 2020 |
  • by Prwatech |
  • 0 Comments

Array:

The array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

·        Array is a collection of similar elements

·        Maintains Insertion Order

·        Immutable

. Once Array is defined, we cannot grow or shrink their size

. However we may change their index

Array is classified into:

. 1-D Array

·   2-D Array

·   Multi-D Array

Program to Display 1-D Array using Concatenation:

package Collection
import Array._
  object A1
  {
    def main(args: Array[String])
    {
      var arr1 = Array(1, 2, 3, 4)
      var arr2 = Array(5, 6, 7, 8)

      var arr3 = concat( arr1, arr2)
      for(x <- arr3)
      {
        println( x )
      }
    }
  }

Program to Display Multidimensional Array :

package Collection
object MultiArr
{
def main(args: Array[String])
{
val arr = Array(Array(2, 4, 6, 8, 10),
Array(1, 3, 5, 7, 9))

for(i<-0 to 1)
{
for(j<- 0 to 4)
{
print(” “+arr(i)(j))
}
println()
}
}
}

Quick Support

image image