Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Have you ever heard about scala as a language slightly similar to java but different? Let's solve all your doubts here😉.
Welcome to yet another article name "Array in Scala." We will learn about Scala first, then move into the main topic.
As we all might know array is a linear data structure that sequentially keeps an element of the same data type. In scala or any other programming language, an array is a group of similar data items.
Scala is a modern multi-paradigm programming language meant to express common programming patterns in a clear, precise, elegant, and type-safe manner.
Martin Odersky is the writer of the scala language.
It supports object-oriented, functional, and high-level programming principles. It's a powerful static-type language. Everything in scala is an object, whether it's a function or a number. It has no concept of primitive data.
A scala array is a group or collection of data items of similar types stored in contiguous memory locations. It enables us to group multiple items of the same data type. They are also used to execute data structures such as heaps, stacks, queues, etc.
The syntax for the scala array:
var arr_name = new Arr[datatype](size)
In the above syntax, type means the data type and arr_name is the name of the array 'arr' you want to form, and size is the number of elements you want to keep in the array.
Need of an Array
In any programming language, arrays are a very useful data structure. Instead of storing these values separately, we can use an array to store the values of multiple variables of the same data type together. We can access each array value individually. For example, if you want to store multiple integer data types, you can use integer-type variables. However, it is not an ideal method for storing these values. We can easily store these values by using an array.
Types of the Scala Array
There are two types of scala array:
Single dimensional array
Multidimensional array
Single Dimensional Array
A single-dimensional array stores elements in linear order. Array elements are kept in a single block of memory. So, if you know the index of an array, you can easily traverse all of its elements.
Syntax
There are many ways:
var arr_name: Arr[arr_type] = new Arr[arr_type](size);
OR
var arr_name = new Arr[arr_type](size)
OR
var arr_name: Arr[arr_type] = new Arr(size);
OR
var arr_name = Arr( 1st element, 2nd element ... nth element)
Example
object HelloWorld{
// Main method.
def main(args: Array[String]) {
var n:Int=0
n=scala.io.StdIn.readInt();
// Declaring single dimensional array.
var Nums = new Array[Int](n)
var i:Int=0
var sum:Int=0
println("Enter array elements:")
while(i<n)
{
Nums(i)=scala.io.StdIn.readInt();
println(Nums(i))
sum=sum+Nums(i)
i=i+1;
}
// Print the sum of the array.
printf("sum of the array is %d ",sum)
println()
}
}
Output:
Time and Space Complexity
Time Complexity- O(1) for each element, overall O(n)
Explanation: We can access the array elements in constant time by using the index value. As a result, the time complexity of accessing an array element is O(1).
Space Complexity- O(n)
Explanation: We have created an array of size n.
Multidimensional Array
A multidimensional array is a type that stores data in matrix form. Depending on the requirements, you can create arrays that range from two to three, four, and many more dimensions. Array syntax is mentioned below.
Syntax
var arr_name = Arr_of_Dim[ArrayType](No_Of_Rows, No_Of_Columns)
OR
var arr_name = Arr(Arr(element...), Arr(element...)
Example
object HelloWorld{
// Main method.
def main(args: Array[String]) {
var n: Int = 0;
n= scala.io.StdIn.readInt();
// Declaring multidimensional array.
var matrix= Array.ofDim[Int](n, n);
var i: Int = 0;
var j: Int = 0;
var sum: Int = 0;
printf("Enter elements of MATRIX:\n");
while (i < n) {
j = 0;
while (j < n) {
matrix(i)(j) = scala.io.StdIn.readInt();
printf("%d ",matrix(i)(j));
sum = sum +matrix(i)(j);
j = j + 1;
}
println()
i = i + 1;
}
// Print the sum of the matrix.
printf("Sum of matrix elements is: %d\n", sum);
}
}
output
Time and Space Complexity
Time Complexity- O(n*m)
Explanation: Where n is the number of arrays (the first dimension) and m is the maximum size of each internal array (the second dimension).
Space Complexity- O(n2)
Explanation: We have created an array of size n*n.
An array is a data structure that sequentially stores an element of the same data type.
What is the purpose of Scala?
Scala is a programming language used in data processing, distributed computing, and web development. It powers many companies' data engineering infrastructure.
What are the main pros of using an array?
The array makes accessing an element easy using the index number.
What is the index of the very first element of the array?
0 is the index of the first element of the array.
What is the main disadvantage of using an array?
The main disadvantage of the array is that you cannot increase the size of the array after the creation of the array.
Conclusion
In this blog, we discussed the scala array with its types and example. Also, we get to know about Scala and its uses. If you are curious about Scala and want to learn more, check out some articles like scala array below: