#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSMutableArray * mtarr = [NSMutableArray array];
NSMutableArray * mtarr1 = [NSMutableArray arrayWithObject:@"one"];
NSMutableArray * mtarr2 = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil];
NSMutableArray * mtarr3 = [NSMutableArray arrayWithCapacity:5];
NSLog(@"%lu", [mtarr3 count]);
[mtarr3 addObject:@"zero"];
NSLog(@"%@", mtarr3);
// 即便设置大小了, 系统不会自动添加 [NSNULL null];
[mtarr3 insertObject:@"three" atIndex:1];
[mtarr3 insertObject:[NSNull null] atIndex:2];
NSLog(@"%@", mtarr3);
// 容量 打印是 3, arrayWithCapacity 那这方法 有什么意义??
NSLog(@"%lu", [mtarr3 count]);
// insert 会替换吗? 不替换, 会把元素往后挪动
[mtarr3 insertObject:@"one" atIndex:1];
NSLog(@"%@", mtarr3);
// 删除
[mtarr3 removeObject:@"zero"];
NSLog(@"%@", mtarr3);
[mtarr3 removeObjectAtIndex:0];
NSLog(@"%@", mtarr3);
[mtarr3 removeAllObjects];
NSLog(@"%@", mtarr3);
// 修改
// 方法1:
[mtarr2 replaceObjectAtIndex:0 withObject:@"zero"];
NSLog(@"%@", mtarr2);
// 方法2: 简单写法 simple way
mtarr2[1] = @"one";
NSLog(@"%@", mtarr2);
// 查找
BOOL isSearch = [mtarr2 containsObject:@"zero"];
if (isSearch) {
NSLog(@"found it");
}
else {
NSLog(@"not found");
}
// 6) 交换元素
[mtarr2 exchangeObjectAtIndex:0 withObjectAtIndex:2];
NSLog(@"%@",mtarr2);
}
return 0;
}
NSMutableArray的使用... exchangeObjectAtIndex
最新推荐文章于 2022-01-14 15:22:32 发布