Scala Tutorial

Basics

Control Statements

OOP Concepts

Parameterized - Type

Exceptions

Scala Annotation

Methods

String

Scala Packages

Scala Trait

Collections

Scala Options

Miscellaneous Topics

Scala | Create Array with Range

In Scala, you can use the range method available in the Array companion object to create an array with a range of integers. Here's how you can use it:

  • Basic Usage:
val arr = Array.range(1, 5)
println(arr.mkString(", "))  // Outputs: 1, 2, 3, 4

Note: The start is inclusive, but the end is exclusive, similar to many range-based functions in other languages.

  • With a Step:

You can also specify a step value:

val arr = Array.range(1, 10, 2)
println(arr.mkString(", "))  // Outputs: 1, 3, 5, 7, 9

In this example, we've created an array starting from 1 to 10 with a step of 2, so it includes every second number.

  • Descending Order:

You can create a range in descending order by providing a start value greater than the end value and giving a negative step:

val arr = Array.range(5, 1, -1)
println(arr.mkString(", "))  // Outputs: 5, 4, 3, 2
  • Using until and by with toArray:

Although not directly a method of the Array object, another common approach in Scala to achieve the same result is using the range operators until (exclusive) or to (inclusive), combined with by for specifying the step, and then converting the result to an array:

val arr1 = (1 until 5).toArray
println(arr1.mkString(", "))  // Outputs: 1, 2, 3, 4

val arr2 = (1 to 5 by 2).toArray
println(arr2.mkString(", "))  // Outputs: 1, 3, 5

Using these methods, you can easily create arrays with ranges of numbers in Scala.

  1. Using Array.range in Scala:

    Array.range creates an array with elements in a specified range.

    val numbers = Array.range(1, 10)
    // Result: Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
    
  2. Generating Arrays with a Range in Scala:

    Use the Range class to generate arrays.

    val numbers = (1 to 5).toArray
    // Result: Array(1, 2, 3, 4, 5)
    
  3. Creating Arrays with until and to Methods in Scala:

    until and to methods define exclusive and inclusive ranges.

    val exclusiveRange = Array.range(1, 5)
    val inclusiveRange = Array.range(1, 5 + 1)
    
  4. Specifying Step Size in Range Arrays in Scala:

    Specify step size using the by method.

    val evenNumbers = Array.range(2, 10, 2)
    // Result: Array(2, 4, 6, 8)
    
  5. Filtering Elements with Range in Scala Array Creation:

    Use a filter condition to select elements.

    val oddNumbers = Array.range(1, 10).filter(_ % 2 != 0)
    // Result: Array(1, 3, 5, 7, 9)
    
  6. Creating Inclusive and Exclusive Range Arrays in Scala:

    Adjust the range to achieve inclusivity or exclusivity.

    val inclusiveRange = (1 to 5).toArray
    val exclusiveRange = (1 until 6).toArray
    
  7. Concatenating Range Arrays in Scala:

    Concatenate range arrays using the ++ operator.

    val numbers1 = Array.range(1, 5)
    val numbers2 = Array.range(6, 10)
    val combined = numbers1 ++ numbers2
    
  8. Using Array.fill with a Range in Scala:

    Use Array.fill with a range to initialize an array with a repeated value.

    val repeatedValues = Array.fill(5)(10)
    // Result: Array(10, 10, 10, 10, 10)