Introduction to Array in Scala
Array, We all will be familiar with this term Array as we are using it since we started programming with java and this collection is so much usable and code friendly that we all must have used it for our programming works.
So let us see can we use this array with Scala programming. So Array being a collection of the same data type, so Scala also provides a Data Structure Array, which stores a fixed-size collection of elements of the same type. The same types comes with as the same data type so that I would be really easy to apply functions over the same data type. The index of Arrays starts with 0 and the last element is the no of elements – 1.
We can hold the data, traverse the elements in the Array, perform operations over the array. We can iterate the array over the length. Scala supports both one dimensional as well as multi-dimension arrays. A single dimension has one row over n columns and for two-dimension it is actually a matrix of (n*m). Since Array as also like an object so whenever an object is created using the “new” new memory space is allocated in the heap and a reference is returned.
Syntax for Array:
var nameofarray= new Array[datatype](size)
nameofarray is the array name that we want to give on to that array, the data type is the type of array data type we want to get for, and the size of the Array we want to have for.
Examples of Array in Scala
Following are the examples are given below:
Example:
var a = new Array[Int];
var days = Array(“Sunday”,”Monday”,”Tuesday”)
Here we have created an array of 1 dimension named days with the days of the week.
1. Multidimensional Array
val nameofarray = Array.ofDim[data_type](number of rows, number of cols)
or
var nameofarray = Array(Array(elements), Array(elements))
Above is the syntax for Multidimensional Array. Here Scala has a method Array.ofDim that is used to create a multidimensional array. With this method, we can create it of upto five dimensions. The other we can do it is Array of Array method which can be used to create the multidimensional arrays.
val b = Array.ofDim[Int](2,3)
This will create a multi-dimension array and we can add elements over the Array.
The same we can make it with the help of the Array of Array method.
Let us see how can we create that.
var arr = Array(Array(0,2,4,6,8),Array(1,3,5,7,9))
This will create a multidimensional array and we can perform operations over that.
2. Insertion of Elements in Array
We can insert elements in an Array and those elements can be used for Array functioning. We can append elements in the array. Also while pointing the indices we put the value over those index in the array.
val a = new Array[Int](10)
a(0) = 1
a(1) = 2
a(2) =3
a(3) =4
This will insert the element in the Array.
For iterating the elements we can use for loop and print the elements in the array. With a simple for loop we can iterate over the array and can traverse the elements in the array.
Val a = Array (0,1,2,3,4,5,6,7,8,9)
for(m <- a)
{
println(m)
}
This will print the elements in the array.
We can also concat or append two array, so that will merge the array value together. We can append the value in the array. +: This will append the value with the Array.
Let us see this with an example:
val a = Array(1,2,3,4,5)
0 +: a :+ 6
This will append the values in the arrays.
The result will be like :- 0,1,2,3,4,5,6
0 +: arr1 :+ 4
Output:
For Multidimensional array we can also do the same.
var arr = Array(Array("a", "b", "c"))
Here we make an multidimensional array and append the other array value to it. ++ is used for appending the multidimensional array.
arr ++= Array(Array("d", "e"))
3. Accessing Element in the Array
We can iterate the elements in Array and can check for all the values over the array. There are several methods with which we can iterate over the array. Let us look on some of them.
4. Iterating the Index
An array can be iterated over the elements with the help of the index they are stored in. The index number iteration gives the elements that are there over the array. So the index will be the length of the array that it is holding on, so just by iterating a loop from the index 0 as the index starts from 0 to going up to the array length, we can traverse the array element.
Let us look over an example:
val a = Array(12,3,4,56,7)
for(i <- 0 until a.length)
{
println(“The index is : ”+i)
println(“The array value is : ”+a(i))
}
Here we can see that with the index values we traversed over the array and can fetch all the values over there. The other way out for traversing an array is iterating the elements itself .its just like we are ignoring the index and directly traversing the elements itself.
Let us have the look over the same example we saw above and try to understand that:-
val a = Array(12,3,4,56,7)
for(i <- 0 until a.length)
{
println(“The index is : ”+i)
println(“The array value is : ”+a(i))
}
for( b <- a )
println(“The Array Element Value is :”+b)
Here we can see that we directly accessed all the elements that were in the Array Ignoring the indexes along.
5. Array Operations
Apart from adding values to traversing and iterating, there are many more operations over the array which we can apply on making the coding challenges easily solved.
Let us check on some operations over Array.
We can concat all the array into a simple array. A simple addition of array over the other one is possible simply with ++ operation over the array.
Let us look over an example:
val c = a:+b
val c = a++b
Here we can see that we can merge the array values over a single array and can take it as one array. The same as we can copy, delete the elements.
For copying the elements we can simply map the identity of the array with a new one.
val d = a map(identity)
There are lots of things that we can do with the help of Array making the work easier over the coding.
Recommended Articles
This is a guide to Array in Scala. Here we also discuss the introduction and syntax of array in scala along with different examples and its code implementation. You may also have a look at the following articles to learn more –
3 Online Courses | 9+ Hours | Verifiable Certificate of Completion | Lifetime Validity
4.5
View Course
Related Courses