首页 > 代码库 > 【Foundation Frame】NSMutableArray
【Foundation Frame】NSMutableArray
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>
voidmultableCreatAray()
{
//创建MultableArray
NSMutableArray * array1 =[NSMutableArrayarrayWithCapacity:10];
NSMutableArray * array2 =[NSMutableArrayarrayWithObjects:@"Hello1",@"Hello2",@"Hello3",@"Hello4",nil];
NSMutableArray * array3 =[NSMutableArrayarrayWithObjects:@"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 = [NSMutableArrayarrayWithObjects:@"one",@"two",@"three",@"four",nil];
NSArray *newAdditions = [NSArrayarrayWithObjects:@"a",@"b",nil];
NSMutableIndexSet *indexes = [NSMutableIndexSetindexSetWithIndex:1];
[indexes addIndex:3];
[array insertObjects:newAdditionsatIndexes:indexes];
NSLog(@"array: %@", array);
}
intmain(intargc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
multableCreatAray();
}
return 0;
// NSMultableArray
//
// Created by mac on 14-12-3.
// Copyright (c) 2014年mac. All rights reserved.
//
#import<Foundation/Foundation.h>
voidmultableCreatAray()
{
//创建MultableArray
NSMutableArray * array1 =[NSMutableArrayarrayWithCapacity:10];
NSMutableArray * array2 =[NSMutableArrayarrayWithObjects:@"Hello1",@"Hello2",@"Hello3",@"Hello4",nil];
NSMutableArray * array3 =[NSMutableArrayarrayWithObjects:@"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 = [NSMutableArrayarrayWithObjects:@"one",@"two",@"three",@"four",nil];
NSArray *newAdditions = [NSArrayarrayWithObjects:@"a",@"b",nil];
NSMutableIndexSet *indexes = [NSMutableIndexSetindexSetWithIndex:1];
[indexes addIndex:3];
[array insertObjects:newAdditionsatIndexes:indexes];
NSLog(@"array: %@", array);
}
intmain(intargc, 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
)
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。