
Copyright © Cay S. Horstmann 2015
val nums = new Array[Int](10)Ten integers, all initialized with zero.
Array without new to specify initial values:
val a = Array("Hello", "World")
The type is inferred.a(0) = "Goodbye"
for (element <- a) traverses the array elementsfor (i <- 0 until a.length) traverses the array indexesArray is a Java array (int[], java.lang.String[]).ArrayBuffer as analog for Java ArrayList/C++ vector:
import scala.collection.mutable.ArrayBuffer val b = new ArrayBuffer[Int]
+= to add at the end:
b += 1 b += (1, 2, 3, 5) b ++= Array(8, 13, 21)
b.insert(2, 6) b.insert(2, 7, 8, 9) b.remove(2) b.remove(3) b.trimEnd(5) ...
val a = b.toArray val b2 = a.toBuffer
for/yieldval a = Array(2, 3, 5, 7, 11) val result = for (elem <- a) yield 2 * elem // result is Array(4, 6, 10, 14, 22)
for (elem <- a if elem % 2 == 0) yield 2 * elem
Array(1, 7, 2, 9).sum // 19
ArrayBuffer("Mary", "had", "a", "little", "lamb").max // "little"
ArrayBuffer(1, 7, 2, 9).sorted // ArrayBuffer(1, 2, 7, 9)
Array(1, 7, 2, 9).reverse // Array(9, 2, 7, 1)
toString works like in Java:
a.toString() // I@b73e5 b.toString() // ArrayBuffer(Hello, World)
mkString for useful results:
a.mkString(", ") // 1, 7, 2, 9
b.mkString("[", ", ", "]") // [Hello, World]
val scores = Map("Alice" -> 10, "Bob" -> 3, "Cindy" -> 8)
scala.collection.mutable.Map if you want the map to be mutable.("Alice", 10).-> operator makes a pair: "Alice" -> 10 yields ("Alice", 10).():
val bobsScore = scores("Bob")
val bobsScore = scores.getOrElse("Bob", 0)scores is mutable, you can assign to scores(key):
scores("Bob") = 20
scores += ("Bob" -> 10, "Fred" -> 7)
scores -= "Alice"
+ and -:
val newScores = scores + ("Bob" -> 10, "Fred" -> 7)
val newScores2 = newScores - "Alice"
The results share most of their structure with the original. for and pattern matching:
for ((k, v) <- scores) println(k + " has score " + v)
for/yield to get a new map:
for ((k, v) <- scores) yield (v, k)
scores.keySet/scores.values.val t = (1, 3.14, "Fred") // An instance of (Int, Double, java.lang.String)
_n:
val second = t._2
val (_, second, third) = t

a(i) < 0. Use until and for/yield.drop in Seq)a.remove(i) for the remaining ones. Except, you need to do that in reverse order. Use for and reverse.val in = new java.util.Scanner(new java.net.URL(
"http://horstmann.com/scala/livelessons/alice30.txt").openStream)
val count = scala.collection.mutable.Map[String, Int]()
while (in.hasNext) {
val word = in.next();
count(word) = ...
}
What is count("Alice")? count("Rabbit")?var count)groupBy method in the Array class. It may not sound like much, but it is really useful in applications. Try this:
val words = Array("Mary", "had", "a", "little", "lamb", "its", "fleece",
"was", "white", "as", "snow", "and", "everywhere", "that", "Mary", "went",
"the", "lamb", "was", "sure", "to", "go")
What do you get for
words.groupBy(_.substring(0, 1))
"New York".partition(_.isUpper)What do you get? Why does the
partition method yield a tuple?partition? (Hint: Pass the function _ < 0.)val symbols = Array("<", "-", ">")
val counts = Array(2, 10, 2)
val pairs = symbols.zip(counts)
What do you get?<<---------->>. That is, repeat the < twice, the - ten times, and the > twice. (Hint: s * n yields the string s repeated n times)