快学scala第三章课后题:
1.编写一段代码,将a设置为一个n个随机整数的数组,要求随机数介于0(包含)和n(不包含)之间.
package KS
import scala.util.Random
/**
* Created by sendoh on 2015/5/2.
*/
object answer1 {
def main(args: Array[String]): Unit ={
makeArr(10).foreach(println)
}
def makeArr(n: Int): Array[Int] = {
val a = new Array[Int](n)
val rand = new Random();
for (i <- a) yield rand.nextInt(n);
}
}
2.编写一个循环,将整数数组中相邻的元素置换。例如,Array(1,2,3,4,5)经过置换后变为Array(2,1,4,3,5).
package KS
/**
* Created by sendoh on 2015/5/3.
*/
object answer2 {
def main(args: Array[String]): Unit ={
val a = Array(1, 2, 3, 4, 5)
revert(a)
a.foreach(println)
}
def revert(arr: Array[Int]): Unit ={
for (i <- 0 until (arr.length - 1, 2)){
val t = arr(i)
arr(i) = arr(i + 1)
arr(i + 1) = t
}
}
}
3.重复前一个练习,不过这一次生成一个新的值交换过的数组。用for/yield.
package KS
/**
* Created by sendoh on 2015/5/3.
*/
object answer3 {
def main(args: Array[String]) {
val a = Array(1, 2, 3, 4, 5)
val b = revertFile(a)
b.foreach(println)
}
def revertFile(arr: Array[Int]): Unit ={
for (i <- 0 until arr.length) yield {
if (i < (arr.length - 1) && i % 2 ==0 ){
val t = arr(i)
arr(i) = arr(i + 1)
arr(i + 1) = t
}
arr(i)
}
}
}
//由于 Scala 具有很多函数式编程的风格,它的许多控制结构都可以具有返回值,例如 if 语句,此时 Scala 中称为 if 表达式。
// 同样,使用 for 表达式也可以具有返回值,这让我们看起来就像是一个有返回值的函数一样。不过想要达到这个目的, 需要使用 yield 关键字,
// 它用于指明在 for 迭代过程中生成一个集合并保存起来,它的语法是:for {子句} yield {循环体}
//尝试用 for – yield 表达式打印一个九九乘法表,代码如下:
//def makeTable() = {
先用 to
//val table = for(row <- 1 to 9) yield {
再用 until
//for(col <- 1 until 10) yield {
//val line = (row * col).toString
数好空格数
//val spaces = " " * (4 - line.length)
//(line + spaces).mkString
//}
//}
换行
//table.mkString("\n")
4.给定一个整数数组,产生一个新的数组,包含元数组中的所有正值,以原有顺序排列,之后的元素是所有零或负值,以原有顺序排列.
package KS
import scala.collection.mutable.ArrayBuffer
/**
* Created by sendoh on 2015/5/3.
*/
object answer4{
def main(args: Array[String]): Unit ={
val a = Array(1, -2, 3, 0, -4, 2, 5, -7)
val b = sigNumArr(a)
println(b)
}
def sigNumArr(arr: Array[Int]): Unit ={
val buf = new ArrayBuffer[Int]()
buf ++= (for (i <- arr if i > 0) yield i)
buf ++= (for (i <- arr if i == 0) yield i)
buf ++= (for (i <- arr if i < 0) yield i)
buf.toArray
}
}
5.如何计算Array[Double]的平均值?
package KS
/**
* Created by sendoh on 2015/5/3.
*/
object answer5 {
def main(args: Array[String]): Unit ={
val a = Array(1.0, 2.3, 4.5, 5.0, 8.4)
val b = avgArr(a)
println(b)
}
def avgArr(arr: Array[Double]): Unit ={
arr.sum / arr.length
}
}
6.如何重新组织Array[Int]的元素将他们以反序排列?对于ArrayBuffer[Int]你又会怎么做呢?
package KS
import scala.collection.mutable.ArrayBuffer
/**
* Created by sendoh on 2015/5/3.
*/
object answer6 {
def main(args: Array[String]): Unit ={
val a = Array(1, -2, 0, 3, 4, -5, 7)
val b = revertArray(a)
b.foreach(println)
//
val c = ArrayBuffer(1, -2, 0, 3, 4, -5, 7)
val d = ArrayBuffer[Int]()
d ++= c.reverse
d.foreach(println)
}
def revertArray(arr: Array[Int]): Unit ={
for (i <- 0 until (arr.length % 2)){
val t = arr(i)
arr(i) = arr(arr.length - 1 - i)
arr(arr.length - 1 - i) = t
}
}
}
7.编写一段代码,产出数组中的所有值,去掉重复项。(提示:查看Scaladoc)
package KS
import scala.collection.mutable.ArrayBuffer
/**
* Created by sendoh on 2015/5/3.
*/
object answer7 {
def main(args: Array[String]): Unit ={
val c = ArrayBuffer(1, -2, 0, 3, 0, 4, -5, 7)
val d = ArrayBuffer[Int]()
d ++= c.distinct
d.foreach(println)
}
}
8.重新编写3.4节结尾的示例。收集负值元素的下标,反序,去掉最后一个下标,然后对每个下标调用a.remove(i)。比较这样做的效率和3.4节中另外两种方法的效率.
package KS
import scala.collection.mutable.ArrayBuffer
/**
* Created by sendoh on 2015/5/3.
*/
object answer8 {
def main(args: Array[String]): Unit ={
val a = Array(1, -2, 0, 3, 0, 4, -5, 7)
val b = delUnFirst(a)
b.foreach(println)
}
def delUnFirst(arr: Array[Int]): Unit ={
val indexes = (for (i <- 0 until arr.length if arr(i) < 0) yield i)
val rights = indexes.reverse.dropRight(1)
val tmp = arr.toBuffer
for (index <- rights) tmp.remove(index)
tmp
}
}
9.创建一个由java.util.TimeZone.getAvailableIDs返回ide时区集合,判断条件是它们在美洲。去掉"America/"前缀并排序.
package KS
/**
* Created by sendoh on 2015/5/3.
*/
object answer9 {
def main(args: Array[String]): Unit ={
var a = timeZoneName()
a.foreach(println)
}
def timeZoneName() = {
val arr = java.util.TimeZone.getAvailableIDs();
val tmp = (for (i <- arr if i.startsWith("America/")) yield { i.drop("America/".length )})
scala.util.Sorting.quickSort(tmp)
tmp
}
}
10.引入java.awt.datatransfer.并构建一个类型为SystemFlavorMap类型的对象:val flavors = SystemFlavorMap.getDefaultFlavorMap().asInstanceOf[SystemFlavorMap] 然后以DataFlavor.imageFlavor为参数调用getNativesForFlavor方法,以Scala缓冲保存返回值。 (为什么用这样一个晦涩难懂的类?因为在Java标准库中很难找到使用java.util.List的代码)
package KS
import java.awt.datatransfer._
import scala.collection.JavaConversions.asScalaBuffer
import scala.collection.mutable
/**
* Created by sendoh on 2015/5/3.
*/
object answer10 {
def main(args: Array[String]): Unit ={
val flavors = SystemFlavorMap.getDefaultFlavorMap().asInstanceOf[SystemFlavorMap]
val buf : mutable.Buffer[String] = flavors.getNativesForFlavor(DataFlavor.imageFlavor)
buf.foreach(println)
}
}