The tuple, Map, and Array of Scala learning

Source: Internet
Author: User
Tags map class

1.Tuple

The Chinese meaning of a tuple is a tuple, and its definition is that no method is required.

For example: Val tup= ("Tuple", "Map", "Array").

It is important to note that when a tuple is indexed, there are a number of different points to the array we see in the usual way, and it is indexed by: underscore and a 1-based element index. (There is a special emphasis on cardinality, because in Java, C + +, when we define arrays, we start with 0)

Object Tupleops {
def Main (args:array[string]): unit={
Val tuples= (2, "Tuple", "Map", "Array")
println (tuples._1)
println (tuples._2)
println (Tuples._3)
}

}

2.Map

Map is the map. We can interpret it as an associative array. Map is a dual set, through the map class construction, with the "-" operator to connect the dual key-value pairs, below we define a mapping set.

For example Valmap=map ("Xiaoming"->1, "Xiaomei"->2, "Xiaofang"->3).

For the index of the map map, we usually use a for loop to assign a key-value pair through (key,value) <-map. It is then displayed by outputting key and value. Object Mapops {
def Main (args:array[string]): unit={
val maps=Map("Xiaoming"->1, "Xiaomei"->2, "Xiaofang"->3)
for ((key,value) <-maps) {
println ("Key is:" +key+, "value is:" +value)
}
println ("____________________________________________________")
for ((key,_) <-maps) {
println ("Key is:" +key)
}
}

}

3.Array

For the array, we are relatively familiar with learning java,c++, and here I will not repeat more. However, I would like to highlight a bit in Scala, where array loops out in two ways.

Arrayops {
Main (args:array[string]): unit={
arrays=Array(1,2,3,4)
println ("First output mode:")
for (i<-0 until Arrays.length) {
println (Arrays (i))
}
println ("____________________________________")
println ("Second output type (recommended):")
for (elem<-arrays) {
println (Elem)
}
}
}

The tuple, Map, and Array of Scala learning

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.