标签:
#define NSLog(FORMAT, ...) fprintf(stderr,"%s\n",[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//可变数组继承不可变数组
//1、创建。设定元素个数进行创建
NSMutableArray *arr=[NSMutableArray arrayWithCapacity:7];
//2、向数组中添加一个元素
NSArray *arr1=@[@"mon",@"tue",@"wed",@"thu",@"fri",@"sat",@"sun"];//不可变数组
NSMutableArray *arr2=[NSMutableArray arrayWithArray:arr1];//创建一个可变数组把不可变数组arr1复制给arr2
[arr2 addObject:@"eig"];
for(id str in arr2)
{
NSLog(@"%@",str);
}
NSLog(@"\n");
//3、根据指定下标插入元素
[arr2 insertObject:@"nine" atIndex:8];
for(id str in arr2)
{
NSLog(@"%@",str);
}
NSLog(@"\n");
//4、删除数组中最后一个元素
[arr2 removeLastObject];
NSLog(@"%@",arr2);
NSLog(@"\n");
//5、根据指定下标删除元素
[arr2 removeObjectAtIndex:7];
NSLog(@"%@",arr2);
NSLog(@"\n");
//6、根据指定对象删除元素
[arr2 removeObject:@"sun"];
NSLog(@"%@",arr2);
NSLog(@"\n");
//7、替换指定下标的元素
[arr2 replaceObjectAtIndex:0 withObject:@"sun"];
NSLog(@"%@",arr2);
NSLog(@"\n");
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/wuduo/p/5122040.html