scalaで配列操作いろいろ

scalaの勉強中。配列操作をまとめた。

参考文献 Scala-スケーラブルプログラミング第3版 p.67

`myList.head`とか`mylist(0)`でいいのでは？`myList.last`とか`mylist(mylist.length -1 )`で事足りる気がする。あったほうが便利なものはつくっとけの精神なのかな？　


```scala
//空リスト
List()
```

    List()

```scala
//空リスト
Nil
```




    List()




```scala
//リストの作成
val thrill = List("Cool", "tools", "rule")
```

    thrill = List(Cool, tools, rule)
    List(Cool, tools, rule)
```scala
//リストの作成
val thrill = "Cool"::"tools"::"rule"::Nil
```


    thrill = List(Cool, tools, rule)
    List(Cool, tools, rule)

```scala
// 添字の要素を返す
val secondOne = thrill(2)
print(secondOne)
```

    rule
    secondOne = rule
    rule




```scala
// 条件に当てはまる要素の数を返す
val count = thrill.count( s => s.length==4 )
print(count)
```

    2
    count = 2
    2




```scala
// 先頭の2つを削除
val droppedList = thrill.drop(2)
println(droppedList)
```

    List(rule)
    droppedList = List(rule)
    List(rule)




```scala
// 末尾の2つを削除
val dropRightList = thrill.dropRight(2)
println(dropRightList)
```

    List(Cool)
    dropRightList = List(Cool)
    List(Cool)




```scala
// 条件に当てはまるものが存在するかture or false を返す
val doesExist = thrill.exists( s => s.length==4 )
println(doesExist)
```

    true
    doesExist = true
    true




```scala
// 条件に当てはまるものだけ返す
val filteredList = thrill.filter(s => s.length==4)
println(filteredList)
```

    List(Cool, rule)
    filteredList = List(Cool, rule)
    List(Cool, rule)




```scala
// すべての要素が条件を満たすか true or false を返す
val forallList = thrill.forall(s=> s.endsWith("l"))
print(forallList)
```

    false
    forallList = false
    false




```scala
// 要素1つ1つに対してprint
thrill.foreach(s => print(s))
```

    Cooltoolsrule


```scala
// 要素1つ1つをprint
thrill.foreach(print)
```

    Cooltoolsrule


```scala
// 先頭の要素を返す
val firstOne = thrill.head
print(firstOne)
```

    Cool
    firstOne = Cool
    Cool




```scala
// 最後の要素を返す
val lastOne = thrill.last
print(lastOne)
```

    rule
    lastOne = rule
    rule




```scala
// 末尾の要素を外した残りの要素を返す
val deleteLastOne = thrill.init
```


    deleteLastOne = List(Cool, tools)
    List(Cool, tools)

```scala
// リストが空かどうか
thrill.isEmpty
```




    false




```scala
// すべての要素に指定の関数を適用したものを返す
val addY = thrill.map(s => s + "y")
print(addY)
```

    List(Cooly, toolsy, ruley)
    addY = List(Cooly, toolsy, ruley)
    List(Cooly, toolsy, ruley)




```scala
// 指定の文字で連結
val joinedString = thrill.mkString(" and ")
print(joinedString)
```

    Cool and tools and rule
    joinedString = Cool and tools and rule
    Cool and tools and rule




```scala
// 条件に合わないものを返す
val notFour = thrill.filterNot( s => s.length == 4)
print(notFour)
```

    List(tools)
    notFour = List(tools)
    List(tools)




```scala
// 逆にする
val reversedList = thrill.reverse
```


    reversedList = List(rule, tools, Cool)
    List(rule, tools, Cool)




```scala
// 条件を満たすよう並べ替え
val sortedList = thrill.sortWith((s, t) => s.charAt(0).toLower < t.charAt(0).toLower)
print(sortedList)
```

    List(Cool, rule, tools)
    sortedList = List(Cool, rule, tools)
    List(Cool, rule, tools)




```scala
// 先頭要素を除いたリストを返す
val tailList = thrill.tail
print(tailList)
```

    List(tools, rule)
    tailList = List(tools, rule)
    List(tools, rule)