What Are Arrays in Scala in Ubuntu 20.04?
Just like any other programming language, an array in Scala holds chunks of data belonging to the same data type. We can have string, integer, character, etc., type arrays in Scala exactly like other programming languages. Moreover, once an array is declared and initialized in Scala, it can easily be manipulated using loops and conditional statements.
How to Use the Scala Arrays in Ubuntu 20.04?
To understand the basics of using the arrays in Scala, you should take a look at the following three examples:
Example # 1: Printing All the Elements of a Scala Array
In this Scala program, we want to teach you the method of printing all the elements of an array in Scala. This program is as follows:
We have declared a Scala class with the name SampleArray in this program. Then, within our “main()” function of the class, we have created an array by using the “var” keyword with the name myArray. Also, we have assigned five different integer values to this array by using the “Array” keyword. After initializing our integer array, we have used a “for” loop with the iterator “x” that iterates till the end of this array. Within this loop, we have used a “println” statement to print the elements of the array on the terminal. We do this by referring to them with the “x” variable that holds the values of all the array indexes in each loop iteration.
For compiling this Scala script, the following command is used:
Then, for executing this Scala program, the command provided below is used:
All the elements of our Scala array printed on the terminal are shown in the following image:
Example # 2: Finding the Sum of All the Elements of a Scala Array
In this Scala program, we want to teach you the method of finding the sum of all the elements of an array in Scala. This program is as follows:
We have declared a Scala class with the name SampleArray in this program. Then, within our “main()” function of the class, we have created an array by using the “var” keyword with the name myArray. Also, we have assigned five different integer values to this array by using the “Array” keyword. After initializing our integer array, we have used a “for” loop with the iterator “x” that iterates all the way to the end of this array. Within this loop, we have used a “println” statement to print the elements of the array on the terminal by referring to them with the “x” variable that holds the values of all the array indexes in each iteration of the loop.
After that, we have declared a variable “sum” and initialized it with the integer “0”. Then, we have a “for” loop that goes till the length of our array. Within this loop, we calculate the sum of all the elements of the array by using the “sum += myArray(i)” notation. Finally, outside this loop, we just printed the value of the “sum” variable on the terminal.
All the elements of the array along with the sum of these elements appeared on the terminal upon the execution of this Scala script, as shown in the following image:
Example # 3: Finding the Maximum Elements of a Scala Array
In this Scala program, we want to teach you the method of printing all the elements of an array in Scala. This program is as follows:
We have declared a Scala class with the name SampleArray in this program. Within our “main()” function of the class, we have created an array by using the “var” keyword with the name myArray. Also, we have assigned five different integer values to this array by using the “Array” keyword. After initializing our integer array, we have used a “for” loop with the iterator “x” that iterates all the way till the end of this array. Within this loop, we have used a “println” statement to print the elements of the array on the terminal by referring to them with the “x” variable that holds the values of all the array indexes in each iteration of the loop.
After that, we declared a variable “max” and assigned the first element of our array. Then, we have a “for” loop that goes till the length of our array. Within this loop, we have a condition to check if the current element of the array is greater than “max”. If this condition holds true, then “max” will be substituted by the existing elements of the array. Lastly, we have printed the value of the “max” variable on the terminal.
All the elements of the array, along with the maximum element out of these, appeared on the terminal upon the execution of this Scala script. In our case, it was “5”, as shown in the following image:
Conclusion
This guide was aimed to teach the readers the usage of arrays in Scala in Ubuntu 20.04. We began with a brief introduction of the arrays in general, followed by three basic examples for printing the elements of an array, finding the sum of the elements of an array, and finding the maximum element out of all the elements of an array. These examples will help you deal with arrays in the Scala programming language. We hope you found this article helpful. Check out other Linux Hint articles for more tips and tutorials.