Array is a special kind of collection in Scala. It is a fixed size data structure that stores elements of the same data type. By using range() method to generate an array containing a sequence of increasing integers in a given range. We can use final argument as jump to create the sequence. if we do not use final argument, then jump would be assumed as 1.
Below are some examples of array with range:
Example:
// Scala program to create array with range import Array._ // Creating object object GFG { // Main method def main(args: Array[String]) { var array1 = range(1, 15) var array2 = range(1, 15, 3) // Print all the elements of array1 for ( i <- array1 ) { print( " " + i ) } println() // Print all the elements of array2 for ( i <- array2 ) { print( " " + i ) } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 4 7 10 13
In above example, an array of range (1, 15). In this range difference is not given so by default difference of range will be 1 element. Elements in the array are 1, 4, 7, 10 and 13. Here, we are creating an array of range (1, 15, 3). Which means an array with elements between 1 and 15 and range difference is 3. Elements in the array are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, and 14.
For some of the collections, such as List and Array, we can also create a Range and convert it to the desired sequence: The REPL shows the array that can be created directly from a Range is toArray. We can also use a Range to create a sequence of characters as below:
Example:
// Scala program to create array with range import Array._ // Creating object object GFG { // Main method def main(args: Array[String]) { val array1 = ('A' to 'F').toArray val array2 = ('a' to 'f').by(2).toArray // Print all the elements of array1 for ( i <- array1 ) { print( " " + i ) } println() // Print all the elements of array2 for ( i <- array2 ) { print( " " + i ) } } } |
A B C D E F a c e
In above example, an array of range (‘A’ to ‘F’).toArray. In this, range difference is not given so by default difference of range will be 1 character. characters in the array are A, B, C, D, E and F. Here, we are creating an array of range (‘a’ to ‘f’).by(2).toArray. Which means an array with characters between a and f and range difference is 2. Characters in the array are a, c, and e.
Recommended Posts:
- Scala Tutorial – Learn Scala with Step By Step Guide
- Program to transform an array of String to an array of Int using map function in Scala
- Scala short <(x: Short): Boolean
- Scala short <(x: Char): Boolean
- Scala Extractors
- Scala | Partially Applied functions
- Scala String indexOf(String str) method with example
- Scala String contentEquals() method with example
- Scala Keywords
- Scala Int /(x: Int) method with example
- Scala Int /(x: Short) method with example
- Program to print Java Set of characters in Scala
- Scala Map size() method with example
- Scala SortedMap addString() method with a start, a separator and an end with example
- Scala Iterator addString() method with example
- Scala String substring(int beginIndex, int endIndex) method with example
- Scala | Functions Call-by-Name
- Program to convert Java list to an iterator in Scala
- Scala Set &() method with example
- Scala | Type Inference
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

