快學Scala習題解答—第一章 基礎

1 基礎 

1.1 在Scala REPL中鍵入3.然後按Tab鍵。有哪些方法可以被應用? 

在scalaREPL中要鍵入"3.",特別要注意"."不要忘記鍵入

下面是結果 
Scala代碼  收藏代碼
  1. !=             ##             %              &              *              +  
  2. -              /              <              <<             <=             ==  
  3. >              >=             >>             >>>            ^              asInstanceOf  
  4. equals         getClass       hashCode       isInstanceOf   toByte         toChar  
  5. toDouble       toFloat        toInt          toLong         toShort        toString  
  6. unary_+        unary_-        unary_~        |  

列出的方法並不全,需要查詢全部方法還是需要到Scaladoc中的Int,Double,RichInt,RichDouble等類中去查看。 

1.2 在Scala REPL中,計算3的平方根,然後再對該值求平方。現在,這個結果與3相差多少?(提示:res變量是你的朋友) 
依次進行計算即可 
Scala代碼  收藏代碼
  1. scala> scala.math.sqrt(3)  
  2. warning: there were 1 deprecation warnings; re-run with -deprecation for details  
  3. res5: Double = 1.7320508075688772  
  4.   
  5. scala> res5*res5  
  6. res6: Double = 2.9999999999999996  
  7.   
  8. scala> 3 - res6  
  9. res7: Double = 4.440892098500626E-16  


1.3 res變量是val還是var? 
val是不可變的,而var是可變的,只需要給res變量重新賦值就可以檢測res是val還是var了 
Scala代碼  收藏代碼
  1. scala> res9 = 3  
  2. <console>:8: error: reassignment to val  
  3.        res9 = 3  
  4.             ^  


1.4 Scala允許你用數字去乘字符串—去REPL中試一下"crazy"*3。這個操作做什麼?在Scaladoc中如何找到這個操作? 
Scala代碼  收藏代碼
  1. scala> "crazy"*3  
  2. res11: String = crazycrazycrazy  

從代碼可以推斷,*是"crazy"這個字符串所具有的方法,但是Java中的String可沒這個方法,很明顯。此方法在StringOps中。 

1.5 10 max 2的含義是什麼?max方法定義在哪個類中? 
直接在REPL中執行 
Scala代碼  收藏代碼
  1. scala> 10 max 2  
  2. res0: Int = 10  

可以看出,此方法返回兩個數字中較大的那個。此方法Java中不存在,所以在RichInt中。 

1.6 用BigInt計算2的1024次方 
簡單的API調用 
Scala代碼  收藏代碼
  1. scala> BigInt(2).pow(1024)  
  2. res4: scala.math.BigInt = 1797693134862315907729305190789024733617976978942306572734300811577326758055009631327084773224  
  3. 075360211201138798713933576587897688144166224928474306394741243777678934248654852763022196012460941194530829520850057688  
  4. 38150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216  


1.7 爲了在使用probablePrime(100,Random)獲取隨機素數時不在probablePrime和Radom之前使用任何限定符,你需要引入什麼? 
so easy. import需要的包啊。Random在scala.util中,而probablePrime是BigInt中的方法,引入即可 
Scala代碼  收藏代碼
  1. import scala.math.BigInt._  
  2. import scala.util.Random  
  3.   
  4. probablePrime(3,Random)  


1.8 創建隨機文件的方式之一是生成一個隨機的BigInt,然後將它轉換成三十六進制,輸出類似"qsnvbevtomcj38o06kul"這樣的字符串。查閱Scaladoc,找到在Scala中實現該邏輯的辦法。 
到BigInt中查找方法。 
Scala代碼  收藏代碼
  1. scala> scala.math.BigInt(scala.util.Random.nextInt).toString(36)  
  2. res21: String = utydx  


1.9 在Scala中如何獲取字符串的首字符和尾字符? 
Scala代碼  收藏代碼
  1. //獲取首字符  
  2. "Hello"(0)  
  3. "Hello".take(1)  
  4. //獲取尾字符  
  5. "Hello".reverse(0)  
  6. "Hello".takeRight(1)  


1.10 take,drop,takeRight和dropRight這些字符串函數是做什麼用的?和substring相比,他們的優點和缺點都是哪些? 

查詢API即可 take是從字符串首開始獲取字符串,drop是從字符串首開始去除字符串。 takeRight和dropRight是從字符串尾開始操作。 這四個方法都是單方向的。 如果我想要字符串中間的子字符串,那麼需要同時調用drop和dropRight,或者使用substring 


博客原文地址Blog URL:http://www.ivanpig.com/blog/?p=452

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章