Arrays

An array is a mutable collection. In arrays, the order of the elements will be preserved and duplicated elements will be kept. Being mutable, you can change the value of any element of the array by accessing it by its index number. Let's demonstrate arrays with several examples. Use the following line of code to just declare a simple array:

val numbers: Array[Int] = Array[Int](1, 2, 3, 4, 5, 1, 2, 3, 3, 4, 5) // A simple array

Now, print all the elements of the array:

println("The full array is: ")
for (i <- numbers) {
print(" " + i)
}

Now, print a particular element: for example, element 3:

println(numbers(2))

Let's sum all the elements and print the sum:

var total = 0;
for (i <- 0 to (numbers.length - 1)) {
total = total + numbers(i)
}
println("Sum: = " + total)

Finding the smallest element:

var min = numbers(0)
for (i <- 1 to (numbers.length - 1)) {
if (numbers(i) < min) min = numbers(i)
}
println("Min is: " + min)

Finding the largest element:

var max = numbers(0);
for (i <- 1 to (numbers.length - 1)) {
if (numbers(i) > max) max = numbers(i)
}
println("Max is: " + max)

Another way of creating and defining an array is using the range () method that goes as follows:

//Creating array using range() method
var myArray1 = range(5, 20, 2)
var myArray2 = range(5, 20)

The preceding line of code means that I have created an array with elements between 5 and 20 with the range difference 2. If you don't specify the 3rd parameter, Scala will assume the range difference is:

//Creating array using range() method without range difference
var myArray1 = range(5, 20, 2)

Now, let's see how to access the elements as follows:

// Print all the array elements
for (x <- myArray1) {
print(" " + x)
}
println()
for (x <- myArray2) {
print(" " + x)
}

It's even possible to concatenate two arrays using the concat() method as follows:

//Array concatenation
var myArray3 = concat( myArray1, myArray2)
// Print all the array elements
for ( x <- myArray3 ) {
print(" "+ x)
}

Note that for using the range() and the concat() method, you will need to import the Scala Array package as follows:

Import Array._

Lastly, it's even possible to define and use a multi-dimensional array as follows:

var myMatrix = ofDim[Int](4,4)

Now, first create a matrix using the preceding array as follows:

var myMatrix = ofDim[Int](4, 4)
// build a matrix
for (i <- 0 to 3) {
for (j <- 0 to 3) {
myMatrix(i)(j) = j
}
}
println()

Print the earlier matrix as follows:

// Print two dimensional array
for (i <- 0 to 3) {
for (j <- 0 to 3) {
print(" " + myMatrix(i)(j))
}
println()
}

The complete source code of the previous example can be seen as follows:

package com.chapter4.CollectionAPI
import Array._ object ArrayExample {
def main(args: Array[String]) {
val numbers: Array[Int] = Array[Int](1, 2, 3, 4, 5, 1, 2, 3, 3, 4, 5)
// A simple array
// Print all the element of the array
println("The full array is: ")
for (i <- numbers) {
print(" " + i)
}
//Print a particular element for example element 3
println(numbers(2))
//Summing all the elements
var total = 0
for (i <- 0 to (numbers.length - 1)) {
total = total + numbers(i)
}
println("Sum: = " + total)
// Finding the smallest element
var min = numbers(0)
for (i <- 1 to (numbers.length - 1)) {
if (numbers(i) < min) min = numbers(i)
}
println("Min is: " + min)
// Finding the largest element
var max = numbers(0)
for (i <- 1 to (numbers.length - 1)) {
if (numbers(i) > max) max = numbers(i)
}
println("Max is: " + max)
//Creating array using range() method
var myArray1 = range(5, 20, 2)
var myArray2 = range(5, 20)
// Print all the array elements
for (x <- myArray1) {
print(" " + x)
}
println()
for (x <- myArray2) {
print(" " + x)
}
//Array concatenation
var myArray3 = concat(myArray1, myArray2)
// Print all the array elements
for (x <- myArray3) {
print(" " + x)
}
//Multi-dimensional array
var myMatrix = ofDim[Int](4, 4)
// build a matrix
for (i <- 0 to 3) {
for (j <- 0 to 3) {
myMatrix(i)(j) = j
}
}
println();
// Print two dimensional array
for (i <- 0 to 3) {
for (j <- 0 to 3) {
print(" " + myMatrix(i)(j))
}
println();
}
}
}

You will get the following output:

The full array is: 1 2 3 4 5 1 2 3 3 4 53 
Sum: = 33
Min is: 1
Max is: 5
5 7 9 11 13 15 17 19 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 5 7 9 11 13 15 17 19 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 2 3
0 1 2 3
0 1 2 3
0 1 2 3

In Scala, lists preserve order, keep duplicated elements, and also check their immutability. Now, let's see some examples of using lists in Scala in the next subsection.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.69.240