03. Arrays - RobertMakyla/scalaWiki GitHub Wiki

Array - has fixed length, but its elements can be reassigned (interview)

new Array[Int](3)     // Array[Int] = Array(0, 0, 0)
new Array[String](3) // Array[String] = Array(null, null, null)

new Array(3) //Array[Nothing] = Array(null, null, null)
            // the type becomes 'Nothing' and elements are null

Array(3) //  Array[Int] = Array(3)

val s = Array("Hello", "World")  // Array[String] = Array(Hello, World)
s(0)                             // Hello
s(1)                             // World
s(0) = "GoodBye cruel"           // s becomes: Array(GoodBye cruel, World)

Ranges

for (i <- range)  instruction              // reminder

for (i <- 100 to (0,-10) ) println(i)       // 100 90 80 70 60 50 40 30 20 10 0

for (i <- r.length -1 to (0,-1) ) print(r(i)) // "12345" --> "54321"

for (i <- (0 until r.length).reverse) print(r(i))   // "12345" --> "54321"
for (i <- (0 to r.length -1).reverse) print(r(i))   // "12345" --> "54321"
"12345" reverse                                   // "12345" --> "54321"

Yield (interview)

for (elem <- 1 to 10 ) yield elem * 2     
                // Vector(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

for(elem <- 1 to 10 if elem % 2 ==0) yield elem   
                // Vector(2, 4, 6, 8, 10)

Many ways of using methods instead of loops:

for(elem <- 1 to 10 if elem % 2 ==0) yield elem * 10
                // Vector(20, 40, 60, 80, 100)

1.to(10).filter( _ % 2 == 0 ).map( _ * 10 )
                // Vector(20, 40, 60, 80, 100)

1.to(10).filter(  i => i%2==0 ).map( _ * 10 )
                // the same

Common Algorithms

Array(1, 2, 3).sum   // 6 - but elements must be numeric
1 to 3 sum           // 6
1 to 3 min           // 1
1 to 3 max           // 6

ArrayBuffer("Mary", "had", "a", "little").max        -->  "little"

Array(1,3,2,4).sortWith (_ < _)                  // Array(1, 2, 3, 4)
Array(1,3,2,4) sortWith {_ < _}                  // Array(1, 2, 3, 4)
Array(1,2,3,4) sortWith ( (a,b)=> a<b )          // Array(1, 2, 3, 4)
Array(1,3,2,4) sortWith ( (a:Int,b:Int) => a<b ) // Array(1, 2, 3, 4)

Quick Sort

val a = Array(1,4,2,5,3,6,5)
scala.util.Sorting.quickSort(a)  // now 'a' is sorted. 

The 'val a' has never had any other object reassigned (so no error)

Making String

1 to 10 mkString              // 12345678910
1 to 10 mkString("<","-",">") // <1-2-3-4-5-6-7-8-9-10>

Array(1,2,3).toString                                     // [I@f8fff2               - Useless java toString()
scala.collection.mutable.ArrayBuffer(1,2,3) toString      // "ArrayBuffer(1, 2, 3)"  -  nice for debugging
scala.collection.mutable.ArrayBuffer(1,2,3) + 4  toString // "ArrayBuffer(1, 2, 3, 4)"

Array (as well as ArrayBuffer) is converted into ArrayOps before any operation on it.

Array(1,2) :+ 3           // Array(1,2,3)

Array(1,2) +: 0           // error !!!
0 +: Array(1,2)           // Array(0,1,2)

Array (1,2) ++ Array(3,4) // Array(1,2,3,4)

Array(1,2,3) diff Array(3,99) // Array(1,2)

Array(1,2,3) contains 3   // true

Multi-D Arrays (declaring without 'new')

val a = Array.ofDim[Int](2,2)   // Array[Array[Int]] = Array(Array(0, 0), Array(0, 0))

Here, the (2,2) are sizes of dimensions, not value !!! It's the same as :

val a = new Array[Array[Int]](2)
for (i <- 0 until a.length) a(i) = new Array[Int](2)

other types:

val a = Array.ofDim[String](2,2) // Array[Array[String]] = Array(Array(null, null), Array(null, null))

val a = Array.ofDim(2,2)   // problem cause type will be Nothing
                           // so anything that we will be able to add, must be Nothing as well

a(0)(0) = 99    // Array(Array(99, 0), Array(0, 0))
a(0)(0) = "99"  // Array(Array(99, null), Array(null, null))

val a = Array.ofDim[Int](2,2)      

Scala to Java (if it's needed)

import scala.collection.JavaConversions.bufferAsJavaList
import scala.collection.mutable.ArrayBuffer
val scalaObject = ArrayBuffer("ls", "-al", "/home/cay")
val javaObject = new ProcessBuilder(scalaObject) // constructor ProcessBuilder(List<String> arg1)

Exercises:

// average
val a = Array[Double](1.0, 2.0) ; val avg = a.sum / a.length ; avg

// duplicates removed
Array(1,2,3,2,1,2,3) distinct
⚠️ **GitHub.com Fallback** ⚠️