Scala learns 3 array-related operation mappings and tuples

Source: Internet
Author: User

I. Fixed-length arraysIf you need an array of the same length, you can use the array in Scala. Val nums = new Array[int] (10)val s = Array ("Hello", "World")
Two. Variable-length array: array buffering
1. val b = Arraybuffer[int] ()   2. Append elements B + = (1,2,3,4,5) 3. Append any set b ++= Array (8,13,21) 4. Removal of the last 5 elements b.trimend (5) 5. Inserting elements B.insert (2,6) 6. Insert any number of elements B.insert (2,7,8,9) 7. Removing elements b.remove (1) 8, removing multiple elements B.remove (2,3,4,5)
three. Iterating through arrays and array buffers 1. Two Elements one jump 0 until (a.length, 2) 2. Start traversing from the end of the array (0 util a.length). Reverse
Mappings and tuples
I. Construct a map 1. Non-variable mappings
Val sources = map ("Alice", "Bob"->3) 2. Variable mapping val scores = new scala.collection.mutable.hashmap[string, int
Two. Get the values in the map Val bobs = Scores ("Bob")//Java-like Scores.get ("Bob") val Bobsscore = Scores.getorelse ("Bob", 0)

The values in the three Update mappings scores ("Bob") = 10//update key "Bob" corresponding to the value scores ("Fred") = 7//Add new key
Scores + = ("Bob", "Fred", 7)//Add multiple relationships
Scores-= "Alice"//Remove a key and corresponding value
var scores = ...; Scores =scores + ("Bob", "Fed"->7)//update can not change collection mappings
Four. Iterative mapping    1.      for ((K,V) <-mapping)     2.      scores.keyset//Similar Java keyset      scores.values //mappings are worth collecting    3.               five. Non-hash mappings         val scores = Scala.collections.immutable.Sortedmap ("Alice"->10, " Fred "_>7"                      six. Interoperability with Java     &NB Sp   Convert Java treemap to Scala mapping type           val  scores:  scala.collection.mutable.map [String,int] = new java.util.treemap[string,int]                   Java . util. Properties go to map[string,string]              Val props:scala.collection.map[string, String] = system.getproprties ()                 seven. Tuple       &NBSP ;   Mapping is a collection of key/value pairs. The simplest form of dual-tuple-----tuplesIs the aggregation of different types of values .          1. Tuples are formed by enclosing a single value in the square brackets             (1,3.14, "Fred ")                          &NBSP;2. If there is a tuple, val t = {1, 2, "FF"}, you can use                val  first = t._1//To assign the first value in the T-tuple to primary, the tuple's members are 1-based two non- 0                        3. For non-required members, you can use _, replace   &nbsp ;            val (First,scond, _) = t
Tuples can be used in cases where the function needs to return more than one value, eight. Zipper operation val Symbols = Array ("<", "-", ">") val Counts = Arry (2,10,2) Val pairs = Sysbols.zip (counts) output dual arrays Array ("<", 2), ("-", 10 ), (">", 2))



























































  




















     

Scala learns 3 array-related operation mappings and tuples

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.