We continue with more Scala on the topic of Arrays in this post. For
all the work in this post, we will use Read-Evaluate-Print-Loop
Scala interpreter. Some familiarity
with Java will be of great help in understanding Scala. In this post, we will
look at how Arrays are defined along with some features of Arrays in Scala.
Three ways to define Arrays in Scala are:
i) With values of the elements that constitute the Array as shown below:
val ArrayWithInt = Array(1,2,3,4,5)
val ArrayWithAny = Array(1,2L,3.0F,4.0D,'5',"Hello")
The results are shown below:
scala> val ArrayWithInt = Array(1,2,3,4,5)
ArrayWithInt: Array[Int] = Array(1, 2, 3, 4, 5)
scala> val ArrayWithAny = Array(1,2L,3.0F,4.0D,'5',"Hello")
ArrayWithAny: Array[Any] = Array(1, 2, 3.0, 4.0, 5, Hello)
ii) define specifying the size as shown below:
var ArrayWithInt:Array[Int] = new Array[Int](5)
To see default values we can use below command:
ArrayWithInt
The results are shown below:
scala> var ArrayWithInt:Array[Int] = new Array[Int](5)
ArrayWithInt: Array[Int] = Array(0, 0, 0, 0, 0)
scala> ArrayWithInt
res155: Array[Int] = Array(0, 0, 0, 0, 0)
iii) Using ofDim command as below:
val ArrayWithInt = Array.ofDim[Int](5)
To see default values we can use below command:
ArrayWithInt
The results are shown below:
scala> val ArrayWithInt = Array.ofDim[Int](5)
ArrayWithInt: Array[Int] = Array(0, 0, 0, 0, 0)
scala> ArrayWithInt
res210: Array[Int] = Array(0, 0, 0, 0, 0)
We can see that the array is initialized with 0s. To populate with values in this case and in earlier case, we can use below command:
for (i <- 0 to ArrayWithInt.length-1) ArrayWithInt(i) = i
ArrayWithInt
The results are shown below:
scala> for (i <- 0 to ArrayWithInt.length-1) ArrayWithInt(i) = i
scala> ArrayWithInt
res214: Array[Int] = Array(0, 1, 2, 3, 4)
Using ofDim, we can also create multidimensional arrays as shown below:
val MultiDimArrayWithInt = Array.ofDim[Int](5,6)
This creates a two dimensional array having five rows and six columns. The results are shown below:
scala> val MultiDimArrayWithInt = Array.ofDim[Int](5,6)
MultiDimArrayWithInt: Array[Array[Int]] = Array(Array(0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0))
Similarly, we can create a three dimensional array as shown below:
val MultiDimArrayWithInt = Array.ofDim[Int](3,3,3)
The results are shown below:
scala> val MultiDimArrayWithInt = Array.ofDim[Int](3,3,3)
MultiDimArrayWithInt: Array[Array[Array[Int]]] = Array(Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0)), Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0)), Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0)))
To access individual elements of an array:
ArrayWithInt(0)
ArrayWithAny(5)
The results are shown below:
scala> ArrayWithInt(0)
res160: Int = 0
scala> ArrayWithAny(5)
res161: Any = Hello
To access with index number, we can use below code:
val ArrayWithAny = Array(1,2L,3.0F,4.0D,'5',"Hello")
for(i <- 0 until ArrayWithAny.length){
println(s"Element at index $i is " + ArrayWithAny(i))
}
The results are shown below:
scala> val ArrayWithAny = Array(1,2L,3.0F,4.0D,'5',"Hello")
ArrayWithAny: Array[Any] = Array(1, 2, 3.0, 4.0, 5, Hello)
scala> for(i <- 0 until ArrayWithAny.length){
| println(s"Element at index $i is " + ArrayWithAny(i))
| }
Element at index 0 is 1
Element at index 1 is 2
Element at index 2 is 3.0
Element at index 3 is 4.0
Element at index 4 is 5
Element at index 5 is Hello
Next, we will see a series of methods that can be applied on Arrays and will be very useful when we work with Arrays. Let us define two arrays that we can use to showcase these methods as shown below:
scala> var a = Array(1,1,2,3,4,5)
a: Array[Int] = Array(1, 1, 2, 3, 4, 5)
scala> var b = Array(6,7,8,9,10,10)
b: Array[Int] = Array(6, 7, 8, 9, 10, 10)
1) ++ : Appends second array to first array
scala> a.++(b)
res220: Array[Int] = Array(1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10)
2) ++: : Appends first array to second array
scala> a.++:(b)
res221: Array[Int] = Array(6, 7, 8, 9, 10, 10, 1, 1, 2, 3, 4, 5)
3) +: : An element is prepended to array
scala> a.+:(100)
res223: Array[Int] = Array(100, 1, 1, 2, 3, 4, 5)
4) :+ : An element is appended to array
scala> a.:+(100)
res225: Array[Int] = Array(1, 1, 2, 3, 4, 5, 100)
5) addString : Takes a StringBuilder and returns a StringBuilder with all elements appended
scala> a.addString(new StringBuilder("Hello"))
res230: StringBuilder = Hello112345
6) addString : Another variant that takes a string separator
scala> a.addString(new StringBuilder("Hello"),":")
res231: StringBuilder = Hello1:1:2:3:4:5
7) contains : Checks if an element is there is array
scala> a.contains(1)
res235: Boolean = true
8) count : Counts elements that match a condition. In below case, it counts the number of 1s. _ signifies any element in Array
scala> a.count(_==1)
res242: Int = 2
9) distinct : Returns distinct elements
scala> a.distinct
res243: Array[Int] = Array(1, 2, 3, 4, 5)
10) drop : Drops first n elements
scala> a.drop(3)
res244: Array[Int] = Array(3, 4, 5)
11) dropRight : Drops last n elements
scala> a.dropRight(3)
res245: Array[Int] = Array(1, 1, 2)
12) dropWhile : Drops longest prefix that satisfies a condition
scala> a.dropWhile(_==1)
res249: Array[Int] = Array(2, 3, 4, 5)
13) exists : Returns boolean if condition is satisfied
scala> a.exists(_==1)
res250: Boolean = true
14) filter : Filters elements that satisfy a condition
scala> a.filter(_%2==0)
res252: Array[Int] = Array(2, 4)
15) filterNot : Opposite of filter
scala> a.filterNot(_%2==0)
res253: Array[Int] = Array(1, 1, 3, 5)
16) find : Finds the first element that satisfies a condition
scala> a.find(_==2)
res254: Option[Int] = Some(2)
17) flatten : Flattens two dimensional arrays to one dimension
scala> val TwoDimArrayWithInt = Array(Array(1,2),Array(3,4))
TwoDimArrayWithInt: Array[Array[Int]] = Array(Array(1, 2), Array(3, 4))
scala> TwoDimArrayWithInt.flatten
res256: Array[Int] = Array(1, 2, 3, 4)
18) forall : Tests if a condition satisfies all elements
scala> a.forall(_ %1==0)
res258: Boolean = true
19) foreach : Applies a function to all elements. We are borrowing the anonymous function from the first post.
scala> a.foreach( (x: Int) => println(x * x))
1
1
4
9
16
25
20) indexOf : Returns index of first occurrence of an element
scala> a.indexOf(3)
res273: Int = 3
21) init : Returns all elements except last
scala> a.init
res274: Array[Int] = Array(1, 1, 2, 3, 4)
22) last : Selects last element
scala> a.last
res277: Int = 5
23) length : Returns length of array
scala> a.length
res278: Int = 6
24) map : Applies a function to all elements of an array
scala> a.map(x => Math.sqrt(x))
res285: Array[Double] = Array(1.0, 1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979)
25) max : Returns maximum in array
scala> a.max
res279: Int = 5
26) min : Returns minimum in array
scala> a.min
res280: Int = 1
27) mkString : Returns a string of all elements
scala> a.mkString
res281: String = 112345
28) mkString : Returns a string of all elements with a separator
scala> a.mkString(":")
res283: String = 1:1:2:3:4:5
29) nonEmpty : Checks if Array is not empty
scala> a.nonEmpty
res286: Boolean = true
30) partition: Partitions based on a condition
scala> a.partition(_ %2==0)
res287: (Array[Int], Array[Int]) = (Array(2, 4),Array(1, 1, 3, 5))
31) product : Returns product of elements
scala> a.product
res288: Int = 120
31) reduce : reduces elements based on an operator as shown below:
scala> a.reduce( (x,y) => (x+y))
res284: Int = 16
32) reverse : Reverses Array
scala> a.reverse
res289: Array[Int] = Array(5, 4, 3, 2, 1, 1)
33) slice : Slices the Array
scala> a.slice(1,3)
res290: Array[Int] = Array(1, 2)
34) sorted : Sorts Array
scala> a.sorted
res291: Array[Int] = Array(1, 1, 2, 3, 4, 5)
35) sum : Sum of elements in Array
scala> a.sum
res292: Int = 16
36) tail : Selects all elements except the first
scala> a.tail
res293: Array[Int] = Array(1, 2, 3, 4, 5)
37) take : Selects first n elements
scala> a.take(2)
res294: Array[Int] = Array(1, 1)
38) toList : Converts to List
scala> a.toList
res300: List[Int] = List(1, 1, 2, 3, 4, 5)
39) toSeq : Converts to Sequence
scala> a.toSeq
res302: Seq[Int] = WrappedArray(1, 1, 2, 3, 4, 5)
40) toVector : Converts to Vector
scala> a.toVector
res303: Vector[Int] = Vector(1, 1, 2, 3, 4, 5)
41) update : Updates the Array
scala> a.update(0,0)
scala> a
res296: Array[Int] = Array(0, 1, 2, 3, 4, 5)
This concludes the discussion of Arrays in Scala.
Three ways to define Arrays in Scala are:
i) With values of the elements that constitute the Array as shown below:
val ArrayWithInt = Array(1,2,3,4,5)
val ArrayWithAny = Array(1,2L,3.0F,4.0D,'5',"Hello")
The results are shown below:
scala> val ArrayWithInt = Array(1,2,3,4,5)
ArrayWithInt: Array[Int] = Array(1, 2, 3, 4, 5)
scala> val ArrayWithAny = Array(1,2L,3.0F,4.0D,'5',"Hello")
ArrayWithAny: Array[Any] = Array(1, 2, 3.0, 4.0, 5, Hello)
ii) define specifying the size as shown below:
var ArrayWithInt:Array[Int] = new Array[Int](5)
To see default values we can use below command:
ArrayWithInt
The results are shown below:
scala> var ArrayWithInt:Array[Int] = new Array[Int](5)
ArrayWithInt: Array[Int] = Array(0, 0, 0, 0, 0)
scala> ArrayWithInt
res155: Array[Int] = Array(0, 0, 0, 0, 0)
iii) Using ofDim command as below:
val ArrayWithInt = Array.ofDim[Int](5)
To see default values we can use below command:
ArrayWithInt
The results are shown below:
scala> val ArrayWithInt = Array.ofDim[Int](5)
ArrayWithInt: Array[Int] = Array(0, 0, 0, 0, 0)
scala> ArrayWithInt
res210: Array[Int] = Array(0, 0, 0, 0, 0)
We can see that the array is initialized with 0s. To populate with values in this case and in earlier case, we can use below command:
for (i <- 0 to ArrayWithInt.length-1) ArrayWithInt(i) = i
ArrayWithInt
The results are shown below:
scala> for (i <- 0 to ArrayWithInt.length-1) ArrayWithInt(i) = i
scala> ArrayWithInt
res214: Array[Int] = Array(0, 1, 2, 3, 4)
Using ofDim, we can also create multidimensional arrays as shown below:
val MultiDimArrayWithInt = Array.ofDim[Int](5,6)
This creates a two dimensional array having five rows and six columns. The results are shown below:
scala> val MultiDimArrayWithInt = Array.ofDim[Int](5,6)
MultiDimArrayWithInt: Array[Array[Int]] = Array(Array(0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0))
Similarly, we can create a three dimensional array as shown below:
val MultiDimArrayWithInt = Array.ofDim[Int](3,3,3)
The results are shown below:
scala> val MultiDimArrayWithInt = Array.ofDim[Int](3,3,3)
MultiDimArrayWithInt: Array[Array[Array[Int]]] = Array(Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0)), Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0)), Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0)))
To access individual elements of an array:
ArrayWithInt(0)
ArrayWithAny(5)
The results are shown below:
scala> ArrayWithInt(0)
res160: Int = 0
scala> ArrayWithAny(5)
res161: Any = Hello
To access with index number, we can use below code:
val ArrayWithAny = Array(1,2L,3.0F,4.0D,'5',"Hello")
for(i <- 0 until ArrayWithAny.length){
println(s"Element at index $i is " + ArrayWithAny(i))
}
The results are shown below:
scala> val ArrayWithAny = Array(1,2L,3.0F,4.0D,'5',"Hello")
ArrayWithAny: Array[Any] = Array(1, 2, 3.0, 4.0, 5, Hello)
scala> for(i <- 0 until ArrayWithAny.length){
| println(s"Element at index $i is " + ArrayWithAny(i))
| }
Element at index 0 is 1
Element at index 1 is 2
Element at index 2 is 3.0
Element at index 3 is 4.0
Element at index 4 is 5
Element at index 5 is Hello
Next, we will see a series of methods that can be applied on Arrays and will be very useful when we work with Arrays. Let us define two arrays that we can use to showcase these methods as shown below:
scala> var a = Array(1,1,2,3,4,5)
a: Array[Int] = Array(1, 1, 2, 3, 4, 5)
scala> var b = Array(6,7,8,9,10,10)
b: Array[Int] = Array(6, 7, 8, 9, 10, 10)
1) ++ : Appends second array to first array
scala> a.++(b)
res220: Array[Int] = Array(1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10)
2) ++: : Appends first array to second array
scala> a.++:(b)
res221: Array[Int] = Array(6, 7, 8, 9, 10, 10, 1, 1, 2, 3, 4, 5)
3) +: : An element is prepended to array
scala> a.+:(100)
res223: Array[Int] = Array(100, 1, 1, 2, 3, 4, 5)
4) :+ : An element is appended to array
scala> a.:+(100)
res225: Array[Int] = Array(1, 1, 2, 3, 4, 5, 100)
5) addString : Takes a StringBuilder and returns a StringBuilder with all elements appended
scala> a.addString(new StringBuilder("Hello"))
res230: StringBuilder = Hello112345
6) addString : Another variant that takes a string separator
scala> a.addString(new StringBuilder("Hello"),":")
res231: StringBuilder = Hello1:1:2:3:4:5
7) contains : Checks if an element is there is array
scala> a.contains(1)
res235: Boolean = true
8) count : Counts elements that match a condition. In below case, it counts the number of 1s. _ signifies any element in Array
scala> a.count(_==1)
res242: Int = 2
9) distinct : Returns distinct elements
scala> a.distinct
res243: Array[Int] = Array(1, 2, 3, 4, 5)
10) drop : Drops first n elements
scala> a.drop(3)
res244: Array[Int] = Array(3, 4, 5)
11) dropRight : Drops last n elements
scala> a.dropRight(3)
res245: Array[Int] = Array(1, 1, 2)
12) dropWhile : Drops longest prefix that satisfies a condition
scala> a.dropWhile(_==1)
res249: Array[Int] = Array(2, 3, 4, 5)
13) exists : Returns boolean if condition is satisfied
scala> a.exists(_==1)
res250: Boolean = true
14) filter : Filters elements that satisfy a condition
scala> a.filter(_%2==0)
res252: Array[Int] = Array(2, 4)
15) filterNot : Opposite of filter
scala> a.filterNot(_%2==0)
res253: Array[Int] = Array(1, 1, 3, 5)
16) find : Finds the first element that satisfies a condition
scala> a.find(_==2)
res254: Option[Int] = Some(2)
17) flatten : Flattens two dimensional arrays to one dimension
scala> val TwoDimArrayWithInt = Array(Array(1,2),Array(3,4))
TwoDimArrayWithInt: Array[Array[Int]] = Array(Array(1, 2), Array(3, 4))
scala> TwoDimArrayWithInt.flatten
res256: Array[Int] = Array(1, 2, 3, 4)
18) forall : Tests if a condition satisfies all elements
scala> a.forall(_ %1==0)
res258: Boolean = true
19) foreach : Applies a function to all elements. We are borrowing the anonymous function from the first post.
scala> a.foreach( (x: Int) => println(x * x))
1
1
4
9
16
25
20) indexOf : Returns index of first occurrence of an element
scala> a.indexOf(3)
res273: Int = 3
21) init : Returns all elements except last
scala> a.init
res274: Array[Int] = Array(1, 1, 2, 3, 4)
22) last : Selects last element
scala> a.last
res277: Int = 5
23) length : Returns length of array
scala> a.length
res278: Int = 6
24) map : Applies a function to all elements of an array
scala> a.map(x => Math.sqrt(x))
res285: Array[Double] = Array(1.0, 1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979)
25) max : Returns maximum in array
scala> a.max
res279: Int = 5
26) min : Returns minimum in array
scala> a.min
res280: Int = 1
27) mkString : Returns a string of all elements
scala> a.mkString
res281: String = 112345
28) mkString : Returns a string of all elements with a separator
scala> a.mkString(":")
res283: String = 1:1:2:3:4:5
29) nonEmpty : Checks if Array is not empty
scala> a.nonEmpty
res286: Boolean = true
30) partition: Partitions based on a condition
scala> a.partition(_ %2==0)
res287: (Array[Int], Array[Int]) = (Array(2, 4),Array(1, 1, 3, 5))
31) product : Returns product of elements
scala> a.product
res288: Int = 120
31) reduce : reduces elements based on an operator as shown below:
scala> a.reduce( (x,y) => (x+y))
res284: Int = 16
32) reverse : Reverses Array
scala> a.reverse
res289: Array[Int] = Array(5, 4, 3, 2, 1, 1)
33) slice : Slices the Array
scala> a.slice(1,3)
res290: Array[Int] = Array(1, 2)
34) sorted : Sorts Array
scala> a.sorted
res291: Array[Int] = Array(1, 1, 2, 3, 4, 5)
35) sum : Sum of elements in Array
scala> a.sum
res292: Int = 16
36) tail : Selects all elements except the first
scala> a.tail
res293: Array[Int] = Array(1, 2, 3, 4, 5)
37) take : Selects first n elements
scala> a.take(2)
res294: Array[Int] = Array(1, 1)
38) toList : Converts to List
scala> a.toList
res300: List[Int] = List(1, 1, 2, 3, 4, 5)
39) toSeq : Converts to Sequence
scala> a.toSeq
res302: Seq[Int] = WrappedArray(1, 1, 2, 3, 4, 5)
40) toVector : Converts to Vector
scala> a.toVector
res303: Vector[Int] = Vector(1, 1, 2, 3, 4, 5)
41) update : Updates the Array
scala> a.update(0,0)
scala> a
res296: Array[Int] = Array(0, 1, 2, 3, 4, 5)
This concludes the discussion of Arrays in Scala.