首页 > 代码库 > OC中数组的用法
OC中数组的用法
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
// 创建数组
NSArray *arr1 = nil;
// NSLog(@"%p", arr1);
NSObject *obj = [NSObject new];
arr1 = [NSArray arrayWithObjects:@"One", @"Two", @"Three", obj, nil];//结束标识,相当于C语言的“\0”
// NSLog(@"%p", arr1);
NSLog(@"arr1: %@", arr1);
// 推荐下面的新语法
NSString *str = @"Hello";
NSArray *arr2 = @[@"zhangsan", @"lisi", @"wangwu", @"zhaoliu" ,@"tianqi", str];//这种方法自动在后面添加了nil
NSLog(@"arr2: %@", arr2);
// 访问数组成员 objectAtIndex
// NSObject *tmpObj = [arr1 objectAtIndex:3];
id tmpObj = [arr1 objectAtIndex:3];
NSLog(@"tmpObj: %@", tmpObj);
// 推荐使用下面的新语法
tmpObj = arr2[2];
NSLog(@"arr2[2]: %@", tmpObj);
// 可变数组
NSMutableArray *firstArr = [NSMutableArray arrayWithCapacity:30];
// 添加
[firstArr addObject:@"One"];
[firstArr addObject:@"Two"];
[firstArr addObject:@"Three"];
[firstArr addObject:@"Four"];
[firstArr addObject:@"Five"];
NSLog(@"The Mutable Array: %@", firstArr);
NSMutableArray *secArr = [NSMutableArray arrayWithArray:arr2];
NSLog(@"secArr: %@", secArr);
// 删除
[secArr removeObject:@"tianqi"];
NSLog(@"==secArr: %@", secArr);
[secArr removeObjectAtIndex:2];
NSLog(@"===secArr: %@", secArr);
// 插入
[secArr insertObject:@"wangwu" atIndex:2];//id 是对象 index 是位置
NSLog(@"after insert: %@", secArr);
// 替换
NSObject *someObj = [NSObject new];
[secArr replaceObjectAtIndex:4 withObject:someObj];
NSLog(@"after replace: %@", secArr);
// C语言遍历数组
NSUInteger number = [secArr count];
for (int i = 0; i < number; i++) {
id obj = secArr[i];
NSLog(@"%@", obj);
}
// 通过枚举器来遍历数组(正着遍历)
NSEnumerator *enum1 = [secArr objectEnumerator];
id obj1;
NSLog(@"*************************");
while (obj1 = [enum1 nextObject]) {
NSLog(@"obj1: %@", obj1);
}
int main(int argc, const char * argv[])
{
// 创建数组
NSArray *arr1 = nil;
// NSLog(@"%p", arr1);
NSObject *obj = [NSObject new];
arr1 = [NSArray arrayWithObjects:@"One", @"Two", @"Three", obj, nil];//结束标识,相当于C语言的“\0”
// NSLog(@"%p", arr1);
NSLog(@"arr1: %@", arr1);
// 推荐下面的新语法
NSString *str = @"Hello";
NSArray *arr2 = @[@"zhangsan", @"lisi", @"wangwu", @"zhaoliu" ,@"tianqi", str];//这种方法自动在后面添加了nil
NSLog(@"arr2: %@", arr2);
// 访问数组成员 objectAtIndex
// NSObject *tmpObj = [arr1 objectAtIndex:3];
id tmpObj = [arr1 objectAtIndex:3];
NSLog(@"tmpObj: %@", tmpObj);
// 推荐使用下面的新语法
tmpObj = arr2[2];
NSLog(@"arr2[2]: %@", tmpObj);
// 可变数组
NSMutableArray *firstArr = [NSMutableArray arrayWithCapacity:30];
// 添加
[firstArr addObject:@"One"];
[firstArr addObject:@"Two"];
[firstArr addObject:@"Three"];
[firstArr addObject:@"Four"];
[firstArr addObject:@"Five"];
NSLog(@"The Mutable Array: %@", firstArr);
NSMutableArray *secArr = [NSMutableArray arrayWithArray:arr2];
NSLog(@"secArr: %@", secArr);
// 删除
[secArr removeObject:@"tianqi"];
NSLog(@"==secArr: %@", secArr);
[secArr removeObjectAtIndex:2];
NSLog(@"===secArr: %@", secArr);
// 插入
[secArr insertObject:@"wangwu" atIndex:2];//id 是对象 index 是位置
NSLog(@"after insert: %@", secArr);
// 替换
NSObject *someObj = [NSObject new];
[secArr replaceObjectAtIndex:4 withObject:someObj];
NSLog(@"after replace: %@", secArr);
// C语言遍历数组
NSUInteger number = [secArr count];
for (int i = 0; i < number; i++) {
id obj = secArr[i];
NSLog(@"%@", obj);
}
// 通过枚举器来遍历数组(正着遍历)
NSEnumerator *enum1 = [secArr objectEnumerator];
id obj1;
NSLog(@"*************************");
while (obj1 = [enum1 nextObject]) {
NSLog(@"obj1: %@", obj1);
}
// 通过枚举器来遍历数组(倒着遍历)
NSEnumerator *enum2 = [secArr reverseObjectEnumerator];
while (obj1 = [enum2 nextObject]) {
NSLog(@"reverse: %@", obj1);
}
NSLog(@"============================");
// 快速枚举
for (id obj in secArr) {
NSLog(@"Fast Enumeration: %@", obj);
}
return 0;
}
while (obj1 = [enum2 nextObject]) {
NSLog(@"reverse: %@", obj1);
}
NSLog(@"============================");
// 快速枚举
for (id obj in secArr) {
NSLog(@"Fast Enumeration: %@", obj);
}
return 0;
}
OC中数组的用法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。