Original Scala Learning: tuple,array,map, file manipulation

Source: Internet
Author: User

1.Tuple: Yuan zu. is a useful container object.

1) Features:

① Yuan zu is immutable, that is, when the declaration of a meta-ancestor, then its length can not be changed;

② can contain different types of data, objects;

③ index is starting with ' _1 ' to read the contents of the meta-ancestor

④ ID: ' () '

2) Code

1   // Ganso 2   def dotuple () {3     //' () ' defines meta-ancestor 4     //  Ganso type val pair: (Int, String, string) pushed down by Scala type 5    println (pair._1)6     println (pair._2) 7   }

   

2.Array: Parameterized array of type

1) Features:

① means to create an instance and complete its "settings", that is, to assign values;

The ②scala array is accessed by an indexed value with parentheses, array (i), and different from the square brackets in Java;

The length of the ③array is immutable, but its value is variable;

2) Code:

1   //Array2 def doarray () {3     //defining an array with the array keyword4Val Array = Array (1, 2, 3, 4, 5)5     //For (i <-0 until Array.Length) {6     //println (Array (i))7     //      }8 9     //enhanced for loop, recommendedTen      for(Elemt <-array) println (ELEMT) One  A}

3.Map

1  //Map Operations2 def domap () {3     //define a map with the map keyword4Val ages = Map ("Jason", "Hadoop", 20))5 6      for(k, V) <-ages) println ("Key is:" + K + ", Hadoop is:" + V)//key Is:jason, Hadoop is:27 key Is:hadoop, Hadoop is:207 8     //"_" is a placeholder9     //For ((k,_) <-ages) println ("Key is:" + k)//key Is:jason Key Is:hadoopTen  One}

Functional programming: Methods No side effects are important ideas for functional style programming, and the calculation and return of values should be the sole purpose of the method.

1. Less coupling between methods, more reliable and easier to reuse

2. (In a static type language) the parameters and return values of the method are checked by the inspector, so it is easier to infer the implied logic error based on the type error

4. Operation of the file: static methods in the source class

1 def dofile () {23     // by Scala.io.Source Object 4     Val file = Source.fromfile ("E:\\java_workspase\\scalaprogramworkspace\\test.txt")5      for (Line <- file.getlines ()) println (line) 6 7   }

Original Scala Learning: tuple,array,map, file manipulation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.