除夕也别忘了学习啊,今天做两题(而且是从后往前做),继续保持新鲜动力学习Scala!

10. 引入java.awt.datatransfer._,构建一个类型为SystemFlavorMap类型的对象:
val flavors = SystemFlavorMap.getDefaultFlavorMap().asInstanceOf[SystemFlavorMap]
然后,以DataFlavor.imageFlavor为参数,调用该对象的getNativesForFlavor方法。
以Scala的Buffer保存返回值,看看是什么。

Ans:

scala> import scala.collection.JavaConversions.asScalaBuffer
import scala.collection.JavaConversions.asScalaBuffer

scala> import scala.collection.mutable.Buffer
import scala.collection.mutable.Buffer

scala> val flavors = SystemFlavorMap.getDefaultFlavorMap().asInstanceOf[SystemFlavorMap]
flavors: java.awt.datatransfer.SystemFlavorMap = java.awt.datatransfer.SystemFlavorMap@19cad9d

scala> val cmd : Buffer[String] = flavors.getNativesForFlavor(DataFlavor.imageFlavor)
cmd: scala.collection.mutable.Buffer[String] = Buffer(PNG, JFIF, DIB, ENHMETAFILE, METAFILEPICT)

scala> cmd
res0: scala.collection.mutable.Buffer[String] = Buffer(PNG, JFIF, DIB, ENHMETAFILE, METAFILEPICT)

9. 创建一个由java.util.TimeZone.getAvailableIDs返回的时区集合,判断条件是它们在美洲,去掉“America/”前缀并排序。
Ans:

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val tz_new = for (tz <- java.util.TimeZone.getAvailableIDs if tz.startsWith("America/")) yield {
    |   tz.drop("America/".length)
    | }
tz_new: Array[String] = Array(Adak, Atka, Anchorage, Juneau, Nome, Sitka, Yakutat, Dawson, Ensenada, Los_Angeles, Metlakatla, Sant
a_Isabel, Tijuana, Vancouver, Whitehorse, Boise, Cambridge_Bay, Chihuahua, Creston, Dawson_Creek, Denver, Edmonton, Hermosillo, In
uvik, Mazatlan, Ojinaga, Phoenix, Shiprock, Yellowknife, Bahia_Banderas, Belize, Cancun, Chicago, Costa_Rica, El_Salvador, Guatema
la, Indiana/Knox, Indiana/Tell_City, Knox_IN, Managua, Matamoros, Menominee, Merida, Mexico_City, Monterrey, North_Dakota/Beulah,
North_Dakota/Center, North_Dakota/New_Salem, Rainy_River, Rankin_Inlet, Regina, Resolute, Swift_Current, Tegucigalpa, Winnipeg, At
ikokan, Bogota, Cayman, Coral_Harbour, Detroit, Fort_Wayne, Grand_Turk, Guayaquil, Havana, Indiana/Indianapolis, Indiana/Marengo,
Indiana/Petersbur...
scala> tz_new.sortWith(_ < _)
res5: Array[String] = Array(Adak, Anchorage, Anguilla, Antigua, Araguaina, Argentina/Buenos_Aires, Argentina/Catamarca, Argentina/
ComodRivadavia, Argentina/Cordoba, Argentina/Jujuy, Argentina/La_Rioja, Argentina/Mendoza, Argentina/Rio_Gallegos, Argentina/Salta
, Argentina/San_Juan, Argentina/San_Luis, Argentina/Tucuman, Argentina/Ushuaia, Aruba, Asuncion, Atikokan, Atka, Bahia, Bahia_Band
eras, Barbados, Belem, Belize, Blanc-Sablon, Boa_Vista, Bogota, Boise, Buenos_Aires, Cambridge_Bay, Campo_Grande, Cancun, Caracas,
Catamarca, Cayenne, Cayman, Chicago, Chihuahua, Coral_Harbour, Cordoba, Costa_Rica, Creston, Cuiaba, Curacao, Danmarkshavn, Dawso
n, Dawson_Creek, Denver, Detroit, Dominica, Edmonton, Eirunepe, El_Salvador, Ensenada, Fort_Wayne, Fortaleza, Glace_Bay, Godthab,
Goose_Bay, Grand_...

或一条语句搞掂:

scala> (for (tz <- java.util.TimeZone.getAvailableIDs if tz.startsWith("America/")) yield tz.drop("America/".length)).sortWith(_ < _)
res6: Array[String] = Array(Adak, Anchorage, Anguilla, Antigua, Araguaina, Argentina/Buenos_Aires, Argentina/Catamarca, Argentina/
ComodRivadavia, Argentina/Cordoba, Argentina/Jujuy, Argentina/La_Rioja, Argentina/Mendoza, Argentina/Rio_Gallegos, Argentina/Salta
, Argentina/San_Juan, Argentina/San_Luis, Argentina/Tucuman, Argentina/Ushuaia, Aruba, Asuncion, Atikokan, Atka, Bahia, Bahia_Band
eras, Barbados, Belem, Belize, Blanc-Sablon, Boa_Vista, Bogota, Boise, Buenos_Aires, Cambridge_Bay, Campo_Grande, Cancun, Caracas,
Catamarca, Cayenne, Cayman, Chicago, Chihuahua, Coral_Harbour, Cordoba, Costa_Rica, Creston, Cuiaba, Curacao, Danmarkshavn, Dawso
n, Dawson_Creek, Denver, Detroit, Dominica, Edmonton, Eirunepe, El_Salvador, Ensenada, Fort_Wayne, Fortaleza, Glace_Bay, Godthab,
Goose_Bay, Grand_...

enter image description here