A collection of Scala basics

If a number loop is used, you can use to or until, for example

Scala > for (I < -0 to 100) println("num:" + I) println("num:" + I)Copy the code

It is also possible to use multiple if statements for multi-layer filtering, for example:

scala> for(i <- 0 to 100 if(i%2)==1) println("num:" + i) scala> for(i <- 0 to 100 | if(i%2 == 1); // Multiple filters, use ";" Separated by | the if (I % 5 > 3) | | println (" num: "+ I) num: 9 num: 19 num: 29 num: 39 num: 49 num: 59 num: 69 num: 79 num: 89, 99: num scala > scala>Copy the code

Three,_The meaning of

1. _ is a wildcard character, similar to * in Java. Such as import Scala.math. _ 2, :_* as a whole, tells the compiler that you want to treat an argument as a sequence of arguments! For example, vals = sum(1 to 5:_*) treats 1 to 5 as a sequence of parameters. Refers to each element of a set. For example, if we want to filter out an even number from Array A and multiply it by 2, we can do the following: a.filter(_%2==0).map(2*_). Sorted= ArrayBufferb (_ 4) val bSorted= b.sorted (_ 4) Such as a. _2. Periods can be replaced with Spaces. 5. Using pattern matching can be used to get members of a tuple, such as val (first, second, third) = t. But if not all parts are needed, use _ on parts that are not needed. For example, in the previous example val(first, second, _) = t 6. Also, the underscore _ represents the default value of a type. For Int, it’s 0. For Double, it’s 0.0 and for reference types, it’s null. \

4, Nothing, Null, None,Unit

<-,=>,->

Traits, like partially implemented interfaces, provide a middle ground between single inheritance and multiple inheritance that can be mixed in with other classes so that a class can be enhanced with a set of features