Package Yjmyzzimport Java.io.PrintWriterimport Java.util.Dateimport scala.io.Sourceobject ScalaApp02 {def main (args: Array[string]) {tupledemo println mapdemo println arraydemo println filewriteandread println (getu Rlcontent ("http://www.cnblogs.com/yjmyzz/")}/** * Tuple example */def Tupledemo = {//val represents a constant (equivalent to final in Java), VAR represents a variable Val tuple = ("Jimmy", +, New Date ())//This is more concise than the tuple<t> in C #//print the first three elements, note that the tuple subscript is starting from 1 println (tuple._1) println (tuple._2) println (tuple._3)}/** * Array example */def Arraydemo = {var Myarr = array (5, 4, 3, 2, 1)//Note Meaning: There is no need for the New keyword for (i <-Myarr) println (i) println//Find out the even var myarr in even = myarr.filter (i = i% 2 = = 0) The simplicity of this syntax is fast catching up to C # LINQ for (i <-even) println (i) println scala.util.Sorting.quickSort (even)//sort for (i &L t;-even) println (i)}//map example Def Mapdemo = {var myMap = Map ("Jimmy", 1), ("Mike", 2), ("John", 3)); Traverse for ((K, v) <-MyMap) { println ("key:" + K + "\t,value=" + V); } println println ("All keys =")//Traverse key for (K <-Mymap.keys) {println (k)} println//Times Calendar value println ("All values =") for (v <-mymap.values) {println (v)} println//Traverse key and value, and A notation (using placeholder notation "_") println ("All keys =") for ((K, _) <-MyMap) println (k) println println ("All Values =&G T ") for ((_, V) <-myMap) println (v)}/** * File read/write example */def Filewriteandread = {val FileName =" scalatest.t XT "//write file var writer = new PrintWriter (fileName) writer.write (" Hello scala\n ") writer.write (" Hello Spark ") Writer.close ()//Read file var filecontent = Source.fromfile (fileName); println (filecontent.mkstring)}/** * Get page content * @param URL * @return */def geturlcontent (url:string): String = {source.fromurl (URL). mkstring}}
As you can see from the example code above, Scala has a very strong type derivation capability, and in most cases, without specifying a variable type, the compiler can deduce the variable type correctly based on the value of the variable, which is no less than C #
Scala Learning Note (02) tuple tuple, array, MAP, file read/write, Web crawl example