标签:foundation nsarray object c
NSArray 代表集合元素不可变的集合,一旦NSArray被创建成功,程序不能向集合中添加新元素,删除旧元素,替换旧元素。
NSMultableArray 恰恰相反,为可变元素的集合,可以增加、删除、替换元素。
可进行的操作:
1、创建 arrayWithCapacity
2、增加元素,以add开头
3、删除元素,以remove开头
4、替换元素,以replace开头
5、对集合本身排序,以sort开头(与NSArray不同,NSMutable是对元素集合自身进行排序,NSArray则是返回一个新的排序完成的元素集合。
(注:以下程序仅供参考,还有很多方法没有列出来)
//
// main.m
// NSMultableArray
//
// Created by mac on 14-12-3.
// Copyright (c) 2014年
mac. All rights reserved.
//
#import
<Foundation/Foundation.h>
void
multableCreatAray()
{
//创建MultableArray
NSMutableArray * array1 =[NSMutableArray
arrayWithCapacity:10];
NSMutableArray * array2 =[NSMutableArray
arrayWithObjects:@"Hello1",@"Hello2",@"Hello3",@"Hello4",
nil];
NSMutableArray * array3 =[NSMutableArray
arrayWithObjects:@"Insert1",@"insert2",@"Insert3",
nil];
//在array1中增加元素
[array1
addObject:@"Hello1"];
[array1
addObject:@"Hello2"];
[array2
addObject:@"Hello5"];
NSLog(@"%@",array1);
NSLog(@"%@",array2);
[array1
addObjectsFromArray:array2];
NSLog(@"%@",array1);
[array1
insertObject:@"Insert1"
atIndex:2];
NSLog(@"%@",array1);
//删除元素
[array1
removeObject:@"Hello1"];
NSLog(@"%@",array1);
NSMutableArray *array = [NSMutableArray
arrayWithObjects:
@"one",
@"two",
@"three",
@"four",
nil];
NSArray *newAdditions = [NSArray
arrayWithObjects:
@"a",
@"b",
nil];
NSMutableIndexSet *indexes = [NSMutableIndexSet
indexSetWithIndex:1];
[indexes
addIndex:3];
[array
insertObjects:newAdditions
atIndexes:indexes];
NSLog(@"array: %@", array);
}
int
main(int
argc,
const
char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
multableCreatAray();
}
return
0;
}
====>>>>>
2014-12-03 17:32:27.148 NSMultableArray[2527:303] Hello, World!
2014-12-03 17:32:27.151 NSMultableArray[2527:303] (
Hello1,
Hello2
)
2014-12-03 17:32:27.152 NSMultableArray[2527:303] (
Hello1,
Hello2,
Hello3,
Hello4,
Hello5
)
2014-12-03 17:32:27.152 NSMultableArray[2527:303] (
Hello1,
Hello2,
Hello1,
Hello2,
Hello3,
Hello4,
Hello5
)
2014-12-03 17:32:27.153 NSMultableArray[2527:303] (
Hello1,
Hello2,
Insert1,
Hello1,
Hello2,
Hello3,
Hello4,
Hello5
)
2014-12-03 17:32:27.153 NSMultableArray[2527:303] (
Hello2,
Insert1,
Hello2,
Hello3,
Hello4,
Hello5
)
2014-12-03 17:32:27.154 NSMultableArray[2527:303] array: (
one,
a,
two,
b,
three,
four
)
Program ended with exit code: 0
【Foundation Frame】NSMutableArray
标签:foundation nsarray object c
原文地址:http://blog.csdn.net/sinat_15735647/article/details/41728033