Help us understand the problem. What is going on with this article?

Swift 配列の基本

More than 3 years have passed since last update.

配列の基本について記載

型指定

var ary:Array<Int> = [1,13]

型指定[]での記述

var ary:[Int] = [1,2]

複数の型指定

var ary:Array<Any> = [1,"anything"]

追加

ary.append("add")

指定indexに追加

ary.insert("add0", atIndex: 0)

index値が1以上3未満の値を置き換える

var animal = ["tiger","elephant","dog","cat"]
animal[1..<3] = ["a","b"]
print(animal) //"["tiger", "a", "b", "cat"]\n"

index値が1~3の値を置き換える

var animal = ["tiger","elephant","dog","cat"]
animal[1...3] = ["a","b"]
print(animal) //"["tiger", "a", "b"]\n"

指定indexを削除

var animal = ["tiger","elephant","dog","cat"]
animal.removeAtIndex(2)
print(animal) // "["tiger", "elephant", "cat"]\n"

コピー
(配列のコピーは参照渡しではなく、値のコピーになります。)

var animal = ["tiger","elephant","dog","cat"]
var copyArray = animal
copyArray[0] = "change"
print(copyArray[0]) // change
print(animal[0])  //tiger

以上

suisuina
Technical director,engineer
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした