// 空の配列の生成
NSMutableArray *a = [[NSMutableArray alloc] init];
NSLog(@"a = %@", a); //=> []
NSLog(@"a.count = %d", [a count]); //=> 0
[a release];
// 値を指定した配列の生成
a = [[NSMutableArray alloc] initWithObjects: @"first", @"second", @"third", nil];
NSLog(@"a = %@", a); //=> [first, second, third]
// 要素の追加
[a addObject: @"fourth"];
NSLog(@"a = %@", a); //=> [first, second, third, fourth]
// 配列による要素の追加
NSMutableArray *temp = [[NSMutableArray alloc] initWithObjects: @"fifth", @"sixth", nil];
[a addObjectsFromArray: temp];
[temp release];
NSLog(@"a = %@", a); //=> [first, second, third, fourth, fifth, sixth]
// 要素の列挙
for (id e in a) {
NSLog(@">> %@", e);
}
// インデックスによる要素の取得
NSLog(@"a[3] = %@", [a objectAtIndex: 3]); //=> fourth
// 配列の最後の要素を削除
[a removeLastObject];
NSLog(@"a = %@", a); //=> [first, second, third, fourth, fifth]
// 配列の先頭の要素を削除
[a removeObjectAtIndex: 0];
NSLog(@"a = %@", a); //=> [second, third, fourth, fifth]
// 指定した要素を削除
[a removeObject: @"third"];
NSLog(@"a = %@", a); //=> [second, fourth, fifth]
// 配列で指定した要素を削除
temp = [[NSMutableArray alloc] initWithObjects: @"second", @"fourth", nil];
[a removeObjectsInArray: temp];
[temp release];
NSLog(@"a = %@", a); //=> [fifth]