3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

The Swift Programming Language "Collection Types"〜Array(配列)〜

Last updated at Posted at 2014-06-09

The Swift Programming LanguageのLanguage Guideの"Collection Types"よりArrayをざっとまとめたものです。

Objective-Cを始め、既に他の言語を習得されてる方向けです。

前章 String and Character
次章 Collection Dictionary

配列

NSArray等と異なり、指定したの型のみしか格納できない。(C,C++,Java的)

配列リテラル

[value1, value2, value3]

var shoppingList: String[] = ["Eggs, "Milk"]
var str: Array<String> = ["abc", "def"] // Array<String>を短くしたものがString[]

// 型推論も可能
var anotherShopplingList = ["Eggs", "Milk"]

アクセスと変更

個数

var shopplingList = ["Eggs", "Milk"]
shoppingList.count // 2
shoppingList.isEmpty // false. 配列が空のときtrue

追加

var shoppingList = ["Eggs", "Milk"]

shoppingList.append("Flour") // 配列の末尾にFlourを追加.
shoppingList += "Baking Powder" // appendと+=は同じ.

参照

Swiftでは配列の添字は、0から始まる

var shoppingList = ["Eggs", "Milk"]
var item = shoppingList[0] // "Eggs"をitemに代入

範囲外の添字を指定した場合、例外送出.

変更

単一の要素

var shoppingList = ["Eggs", "Milk"]
var item = shoppingList[0]
shoppingList[0] = "six eggs"

item // "Eggs"
shoppingList[0] // "six eggs"

範囲外の添字を指定した場合、例外送出.

範囲

var shoppingList = ["Eggs", "Milk", "Flour"]
shoppingList[1...2] = ["milk", "flour"] // ["Eggs", "milk", "flour"]になる

範囲外の添字を指定した場合、例外送出
末尾が範囲外になる場合も、例外送出

挿入

var shoppingList = ["Eggs", "Milk"]
shoppingList.insert("Flour", atIndex:0)

範囲外の添字を指定した場合、例外送出

削除

var shoppingList = ["Eggs", "Milk"]
shoppingList.removeAtIndex(0) // 0番目の要素を削除する.

shoppingList.removeList() // 末尾を削除する.

探索

基本形

var shoppingList = ["Eggs", "Milk", "Flour"]

// shoppingListを順に探索する.
for item in shoppingList {
  println(item)
}

添字も必要な場合

enumerate関数を利用する.

var shoppingList = ["Eggs", "Milk", "Flour"]

// shoppingListを順に探索する.
for (index,item) in enumerate(shoppingList) {
  println("i:\(i) \(item)")
}

生成と初期化

// 要素0
var someInts = Int[]()

// 指定した要素を埋めて生成
var threeDoubles = Double[](count: 3, repeatedValue:0.0) // [0.0, 0.0, 0.0]

// 型推論
var threeDoubles2 = Array(count: 3, repeatedValue:1.25) // [1.25, 1.25, 1.25]

連結

var threeDoubles = Double[](count: 3, repeatedValue:0.0)

var threeDoubles2 = Array(count: 3, repeatedValue:1.25)

var mergedDoubles = threeDoubles + threeDoubles2 // [0.0,0.0,0.0,1.25,1.25,1.25]

配列はJavaでいう参照型っぽい

var threeDoubles = [0.0, 0.0, 0.0]
var threeDoubles2 = threeDoubles

threeDoubles[0] = 1.25
threeDoubles2[0] // 1.25  // threeDoublesを変更すると、threeDoubles2の内容も変更される。

let

配列をletの変数に格納すると、要素は変更できるけど、
要素数は変更できない。

var vals = [1,2,3]
let vals2 = vals
vals2[0] = 0 // これはOK
vals2 += 4 // コンパイルエラー

次章 Collection Dictionary

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?