Created
February 17, 2013 07:29
-
-
Save ozw-sei/4970545 to your computer and use it in GitHub Desktop.
Scala勉強中@コップ本 コレクション ref: http://qiita.com/items/58e766b615e9f16812e3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//要素が三つのString型配列を作成 | |
val array = Array\[String](3) | |
//こんな風に代入して使えます。 | |
//配列はかならず、ミュータブルな構造で生成されます。 | |
array(0) = "1" | |
//配列要素のアクセスの添え字は()を使用します。 | |
array(1) = "2" | |
array(2) = "3" | |
//配列の要素を出力 | |
array.foreach(println) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1という要素を持つ新しいリストを作成 | |
val one = List(1) | |
val two = List(2) | |
val three = List(3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//twoというリストが持つ :: メソッドを呼び出し、one と two を連結した新しいリストを返す | |
val oneTwo = one ::: two | |
val oneTwoThree = oneTwo ::: three | |
or | |
//なんこでok | |
val oneTwoThree = one ::: two ::: three | |
//出力 | |
oneTwoThree.foreach(println) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1,2,3,の要素を持つリストを生成 | |
val oneTwoThreeFour = 1 :: 2 :: 3:: Nil | |
//Nilないと駄目だよ! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//とりま、リストつくります | |
val Container = List("One","Two","Three","Four") | |
//リストの添え字2(先頭は1)を返す | |
Container(2) | |
//要素数を返す | |
Container.count | |
//コンテナの中で文字数が3のものを返す | |
Container.count(s=>s.length == 4) | |
//先頭の二要素を取り除いたリストを返す | |
Container.drop(2) | |
//末尾の二要素を取り除いたリストを返す | |
Container.dropRight(2) | |
//配列の要素の中に"One"要素があるかどうかを返す | |
Container.exist(s=>s === "One") | |
//長さが4のすべての要素を順にならべたリストを返す | |
Container.filter(s->s.length == 4) | |
//リストのすべての要素の末尾が"O"になっているかどうかを返す | |
Container.forall(s=>s.endsWith("l")) | |
//リストの中身を一つ一つ走査する | |
Container.foreach(println) | |
//リストの先頭要素を返す | |
Container.head | |
//末尾を取り除いた残りを返す | |
Container.init | |
//リストが空かどうかを返す | |
Container.isEmpty | |
//リストの最後の要素を返す | |
Container.last | |
//リストの要素数を返す | |
Container.length | |
//リストの要素を並べた文字列をつくる | |
Container.mkString | |
他にもいろいろ、便利なメソッドを持ってます・・・ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val shop = ("21",forever) | |
//ちょっと変わったアクセス方法です。 | |
//__添え字は1から始まります。__ | |
println(shop._1) | |
println(shop._2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment