Scala Single dimensionalArray

Category : Scala | Sub Category : Scala Programs | By Runner Dev Last updated: 2020-10-08 15:48:19 Viewed : 209


Scala Single dimensionalArray

 Array is a collection of mutable values. It is an index based data structure which starts from 0 index to n-1 where n is length of array.

final classArray[T] extends java.io.Serializable with java.lang.Cloneable 

Arrays are mutable, indexed collections of values. Array[T] is Scala(s) representation for Java(s) T[].

1.   val numbers = Array(1, 2, 3, 4)

2.   val first = numbers(0) // read the first element

numbers(3) = 100 // replace the 4th array element with 100

3.   val biggerNumbers = numbers.map(_ * 2) // multiply all numbers by two 

Arrays make use of two common pieces of Scala syntactic sugar, shown on lines 2 and 3 of the above example code. Line 2 is translated into a call to apply(Int), while line 3 is translated into a call to update(Int, T).

The conversion to ArrayOps takes priority over the conversion to ArraySeq. For instance, consider the following code:

val arr = Array(1, 2, 3)

val arrReversed = arr.reverse

val seqReversed : collection.Seq[Int] = arr.reverse

 ยท        Syntax for Single Dimensional Array

var arrayName : Array[arrayType] = new Array[arrayType](arraySize);   or 

var arrayName = new Array[arrayType](arraySize)  or  

var arrayName : Array[arrayType] = new Array(arraySize);   or  

var arrayName = Array(element1, element2 ... elementN)   

Example:

    Following example illustrates about Scala single dimensional Array

    Save the file as โˆ’  SingleDimeArray.scala. 

SingleDimeArray.scala

package runnerdev

class ArrayDeclare {

  var arr = Array(1, "cat", "dog", 4, 5) // Creating single dimensional array

  def show() {

    for (a <- arr) // Traversing array elements

      println(a)

    println("Third Element is  " + arr(2)) // Accessing elements by using index

  }

}

object SingleDimeArray {

  def main(args: Array[String]) {

    var a = new ArrayDeclare()

    a.show()

  }

}  

compile and run the above example as follows 

scala> scalac SingleDimeArray.scala

scala> scala SingleDimeArray 

Output:

1

cat

dog

4

5

Third Element is dog  

Example 2:

     Following example illustrates about Scala Sinlge dimensional Array

      Save the file as โˆ’  SingleDimeArray2.scala 

SingleDimeArray2.scala 

package runnerdev

class ArrayDeclare2 {

  var arr = new Array[Int](5) // Creating single dimensional array

  arr(0) = 10

  arr(1) = 11;

  arr(2) = 12;

  arr(3) = 13;

  def show() {

    for (x <- arr) { // Traversing array elements

      println(x)

    }

    println("Third Element before assignment = " + arr(3)) // Accessing elements by using index

    arr(3) = 11 // Assigning new element at 2 index

    println("Third Element after assignment = " + arr(3))

  }

} 

object SingleDimeArray2 {

  def main(args: Array[String]) {

    var arrayObj = new ArrayDeclare2()

    arrayObj.show()

  }

}

compile and run the above example as follows 

scala> scalac ArrayDeclare2.scala

scala> scala ArrayDeclare2

Output:

10

11

12

13

0

Third Element before assignment = 13

Third Element after assignment = 11

Search
Sub-Categories
Related Articles

Leave a Comment: