陣列練習題

2gua發表於2013-02-09

除夕也別忘了學習啊,今天做兩題(而且是從後往前做),繼續保持新鮮動力學習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

相關文章