Scala Collections - Array with Range



Use of range() method to generate an array containing a sequence of increasing integers in a given range. You can use the final argument as step to create the sequence; if you do not use the final argument, then step would be assumed as 1.

Let us take an example of creating an array of range (10, 20, 2): It means creating an array with elements between 10 and 20 and range difference 2. Elements in the array are 10, 12, 14, 16, and 18.

Another example: range (10, 20). Here range difference is not given so by default it assumes 1 element. It creates an array with the elements in between 10 and 20 with range difference 1. Elements in the array are 10, 11, 12, 13, ..., and 19.

Syntax

To create an array with a specified range and step, you can use the Array.range method with three arguments: the start value, the end value, and the step value.

Syntax with Step

val arrayName: Array[Int] = Array.range(start, end, step)

Syntax without Step

val arrayName: Array[Int] = Array.range(start, end)

1. Creating an Array with Range

You can create arrays using a range of values in Scala. This is used for sequences of numbers without explicitly specifying each element.

Example

For example, you can create an array of integers from 1 to 10 using the Array.range method, like this:

object Demo {
   def main(args: Array[String]): Unit = {
      val rangeArray: Array[Int] = Array.range(1, 11)
      println(rangeArray.mkString(", "))
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

2. Using Array.fill with Ranges

You can also use the Array.fill method in combination with ranges to initialize an array with specific values.

Example

For example, creating an array of 10 elements, all initialized to 42:

object Demo {
   def main(args: Array[String]): Unit = {
      val fillArray: Array[Int] = Array.fill(10)(42)
      println(fillArray.mkString(", "))
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

42, 42, 42, 42, 42, 42, 42, 42, 42, 42

3. Using Array.tabulate with Ranges

You to create an array and initialize its elements based on a function using Array.tabulate method.

Example

For example, you can create an array where each element is the square of its index:

object Demo {
   def main(args: Array[String]): Unit = {
      val tabulateArray: Array[Int] = Array.tabulate(10)(n => n * n)
      println(tabulateArray.mkString(", "))
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

0, 1, 4, 9, 16, 25, 36, 49, 64, 81

4. Printing Arrays with Ranges

You can print array in Scala easily. You can use the mkString method to convert the array elements into a string separated by commas, or any other delimiter.

Example

object Demo {
   def main(args: Array[String]): Unit = {
      val rangeArray: Array[Int] = Array.range(1, 11)
      println(rangeArray.mkString(", "))
   }
}

For multidimensional arrays, you can use nested loops or the foreach method to print the elements in a formatted way.

Example

object Demo {
   def printArray[T](array: Array[T]): Unit =
   println(array.mkString(", "))

   def main(args: Array[String]): Unit = {
      val array1D: Array[Int] = Array.range(1, 11)
      printArray(array1D)
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Example Usage in Scala Programs

This is simple example to create and print arrays with ranges in a Scala program:

object Demo {
   def main(args: Array[String]): Unit = {
      // Creating arrays with ranges
      val rangeArray = Array.range(1, 11)
      val evenArray = Array.range(2, 21, 2)

      // Printing arrays
      println("Range Array: " + rangeArray.mkString(", "))
      println("Even Array: " + evenArray.mkString(", "))
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

Range Array: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Even Array: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20

Arrays with ranges are useful for generating sequences of values in Scala.

Advertisements