#import <Foundation/Foundation.h>
#import "Person.h"
#import "Dog.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Person *xw = [[Person alloc] init];
Person *zs = [[Person alloc] init];
Dog *ww =[[Dog alloc] init];
Dog *hz =[[Dog alloc] init];
//数组的创建
NSArray *array = @[@"pxd", xw, zs, ww, hz];
NSArray *array2 = @[array, @"zp", @"wc"];
NSLog(@"%@", array2);
//创建一个空的数组
NSArray *array3 = [NSArray array];
NSArray *array4 = [[NSArray alloc] init];
NSArray *array5 = [NSArray arrayWithArray:array];
NSArray *array6 = [[NSArray alloc] initWithArray:array];
NSArray *array7 = [NSArray arrayWithObject:xw];
//创建的时候初始化一部分数据
//数据结尾为nil 不能向数组里面添加一个值为nil的对象
NSArray *array8 = [NSArray arrayWithObjects:xw, zs, nil];
NSArray *array8_1 = [[NSArray alloc] initWithObjects:xw, zs, nil];
NSLog(@"....%@", array8);
//1.获取数组元素个数 count
NSLog(@"the count of array8 is %ld", [array8 count]);
//2.访问数组里面的某个元素 使用索引值访问index
Person *temp = [array8 objectAtIndex:0];
//3.获取数组里面的最后一个元素
Person *temp2 = [array8 lastObject];
Person *temp3 = [array8 firstObject];
//4.如何遍历一个数组
for (int i = 0; i < array8.count; i++){
NSLog(@"%@", [array8 objectAtIndex:i]);
}
//使用快速循环
for (Person *p in array8){
NSLog(@"%@", p);
}
//获取一个对象的索引值
NSInteger index = [array8 indexOfObject:@"pxdafafa"];
NSLog(@"index:%ld", index);
//判断数组里面是否包含某个元素
if ([array8 containsObject:@"pxdadfafa"]) {
NSLog(@"YES");
} else{
NSLog(@"NO");
}
//NSMutableArray可变数组
NSMutableArray *marray1 = [NSMutableArray array];
NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:3];
//增加一个元素
[mArray addObject:xw];
[mArray addObject:zs];
//交换元素
[mArray exchangeObjectAtIndex:0 withObjectAtIndex:1];
//插入一个元素
[mArray insertObject:array5 atIndex:0];
NSLog(@"%@", mArray);
//删除一个元素
[mArray removeObject:xw];
[mArray removeObjectAtIndex:0];
NSLog(@"%@", mArray);
//删除所有
[mArray removeAllObjects];
}
return 0;
}