# Ensemble

### Tableau

``````
package org.example

object ArrayApp extends App{
//println("hello")
val a = new Array[String](5)
a(0)="hello"
println(a(0))

val b = Array("hello","world")

val c = Array(1,2,3,4,5,67)
c.sum
c.max
c.mkString("/")

}

``````

``````  val d=scala.collection.mutable.ArrayBuffer[Int]()

d+=1
d+=2
d+=(2,33,4)
d++=Array(33,45,22)
println(d+"-------------------")
d.insert(0,999)
d.remove(1,2)
d.trimEnd(2)
println(d+"-------------------")
// Transformé en immuable
d.toString()
for(i<-0 until d.length){

println(c(i))
}

``````

hello
ArrayBuffer(1, 2, 2, 33, 4, 33, 45, 22)-------------------
ArrayBuffer(999, 2, 33, 4, 33)-------------------
1
2
3
4
5

### List

listEst immuable,C'est exact.list Les opérations telles que l'ajout, la suppression ou la prise de valeur renvoient une nouvelle list.

``````scala> Nil
res4: scala.collection.immutable.Nil.type = List()
``````
``````
scala> Nil
res4: scala.collection.immutable.Nil.type = List()

scala> val l= List(1,2,3,4,5,56)
l: List[Int] = List(1, 2, 3, 4, 5, 56)

res5: Int = 1

scala> l.tail
tail   tails

scala> l.tail
res6: List[Int] = List(2, 3, 4, 5, 56)

scala> l.tails
res7: Iterator[List[Int]] = non-empty iterator

scala>

``````

``````
val d=scala.collection.mutable.ArrayBuffer[Int]()

d+=1
d+=2
d+=(2,33,4)
d++=Array(33,45,22)
d++ =List(1,2,3,4,)

``````

### Set

set C'est une collection non répétitive , S'il y a des données en double , Il va automatiquement se désintégrer .

``````scala> val set = Set(1,2,3,1,2,5)
set: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 5)

``````

### Map

map- Oui.K-VEnsemble de paires de valeurs clés.

``````package org.example

object MapApp {

def main(args: Array[String]): Unit = {

val map = Map(
"1" -> "hello" ,
2 -> "world",
3 -> "!!!!!"
)
println(map.mkString(","))

println("-----------------------")
for(x<-map){

println(x._1+":"+x._2)
}

println("-----------------------")

var keys = map.keys
var keyIterator = keys.iterator
while(keyIterator.hasNext) {

val key = keyIterator.next()
println(key + "\t" + map.get(key).get)
}

}

}

``````
`````` 1 -> hello,2 -> world,3 -> !!!!!
-----------------------
1:hello
2:world
3:!!!!!
-----------------------
1	hello
2	world
3	!!!!!

``````

### Optuon&Some&None

``````    val map = Map(
"1" -> "hello" ,
2 -> "world",
3 -> "!!!!!"
)

println(map.get(2))
println(map.get(999))

``````

Some(world)
None

option.scala

``````
@SerialVersionUID(5066590221178148012L) // value computed by serialver for 2.11.2, annotation added in 2.11.4
case object None extends Option[Nothing] {

def isEmpty = true
def get = throw new NoSuchElementException("None.get")
}

@SerialVersionUID(1234815782226070388L) // value computed by serialver for 2.11.2, annotation added in 2.11.4
final case class Some[+A](x: A) extends Option[A] {

def isEmpty = false
def get = x
}

``````

### Tuple

Comme la liste , Contrairement aux listes, les tuples peuvent contenir différents types d'éléments . Les valeurs des tuples sont construites en incluant des valeurs individuelles entre parenthèses . Le processus de création peut ajouter newMots clés, Pas du tout. .

``````

package org.example

object TupleApp {

def main(args: Array[String]): Unit = {

var t=new Tuple3[Int,Int,String](1,99,"hello")
println(t.toString())

println("----------------")

var t2=(9999,"hello")

println(t2.toString())

println(t2.swap.toString())

}

}

``````
``````
(1,99,hello)
----------------
(9999,hello)
(hello,9999)

``````

# Correspondance des motifs

### Type de base

Java ： Faire un jugement conditionnel sur une valeur, par exemple `switch`

La correspondance des motifs est similaire à javaDeswitch case.Scala La correspondance Pattern peut correspondre non seulement aux valeurs, mais aussi aux types 、 Correspondance de haut en bas , Si elle correspond, elle ne correspond plus 、 Quand ça ne correspond pas ,Ça correspond àcase _ ,équivalent àdefault、match De l'extérieur ”{ }” Peut être supprimé comme une déclaration .

``````
def match_test(m:Any) = {

m match {

case 1 => println("nihao")
case m:Int => println("Int")
case _ => println("default")
}
}

``````
``````package org.example

object MarchApp {

def main(args: Array[String]): Unit = {

case "B" => println("Just so so")
case "A" => println("good")
case "S" => println("cool")
case _=> println("No.1")
}

}
}

}

```scala
> cool
good
No.1

### Array

```scala

def greeting(array:Array[String]) = {

array match {

case Array("zs")=> println("hi,zs")
case Array(x,y)=> println(x+"and"+y)
case Array("zs",_*)=>println("zs and other")
case _=>println("everyone")
}
}

greeting(Array("zs"))
greeting(Array("zs","ls"))

``````

hi,zs
zsandls

### List

``````    def greeting1(list:List[String]) = {

list match {

case "zs"::Nil=> println("hi,zs")
case x::y::Nil=> println(x+"and"+y)
case "zs"::tail =>println("zs and other")
case _=>println("everyone")
}
}

greeting1(List("zs"))
greeting1(List("zs","ls"))
}

``````

### Correspondance de type

``````
def matchType(obj: Any) = {

obj match {

case x: Int => println("hi,int")
case y: String => println(y)
case m: Map[_, _] => println("map")
case _ => println("everyone")
}
}

matchType(Map(1 -> "yes"))
matchType(11)
matchType("hello")
}

``````

### Gestion des exceptions

``````
try{

val i=10/0
println(i)

}catch {

case e:ArithmeticException=>println(e.getMessage)
case e:Exception=>println(e.getMessage)
}finally {

}

``````

/ by zero

https://cdmana.com/2021/10/20211013211945061o.html

Scroll to Top