// OC的數組
// OC裏的數組存放的是對像
NSArray *arr = [[NSArray alloc] init];
// count:數組的元素個數
NSLog(@"%ld", arr1.count);
// OC數組也通過下標取值
NSLog(@"%@", [arr1 objectAtIndex:0]);
NSLog(@"%@", arr1[0]);
// 判斷是否有指定字符串
NSLog(@"%d", [arr1 containsObject:@"2"]);
// 向數組內添加對象
Student *stu1 = [[Student alloc] initWithName:@"張三"];
Student *stu2 = [[Student alloc] initWithName:@"李四"];
Student *stu3 = [[Student alloc] initWithName:@"王二麻子"];
NSArray *arr = [[NSArray alloc] initWithObjects:stu1, stu2, stu3, nil];
NSArray *arr = [NSArray arrayWithObjects:stu1, stu2, stu3, nil];
// 快速枚舉:能快速遍歷數組等容器對象
// 都是對容器裏對象的遍歷
// 爲了增加代碼可讀性,避免不必要的錯誤, 儘量讓forin的前部分類型和數組內元素的類型相同
for (NSString *str in arr) {
NSLog(@"%@", str);
}
// 遍歷學生姓名
Student *stu1 = [[Student alloc] initWithName:@"張三"];
Student *stu2 = [[Student alloc] initWithName:@"李四"];
Student *stu3 = [[Student alloc] initWithName:@"王二麻子"];
NSArray *arr = @[stu1, stu2, stu3];
for (Student *stu in arr) {
NSLog(@"%@", stu.name);
}
// 可變數組
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4" ,nil];
// 添加一個字符串
[arr addObject:@"5"];
NSLog(@"%@", arr);
// 移除下標2的字符
[arr removeObjectAtIndex:2];
NSLog(@"%@", arr);
// 插入一個字符串
[arr insertObject:@"6" atIndex:3];
NSLog(@"%@", arr);
// 替換一個字符串
[arr replaceObjectAtIndex:3 withObject:@"7"];
NSLog(@"%@", arr);
// 交換兩個字符串
[arr exchangeObjectAtIndex:3 withObjectAtIndex:4];
NSLog(@"%@", arr);
// 清除數組
[arr removeAllObjects];
NSLog(@"%@", arr);