OC 數(shù)組特點(diǎn): 可以存儲不同類型的對象,oc的數(shù)組 只能存儲對象
數(shù)組可以遍歷,占用的內(nèi)存空間是連續(xù)的. oc中的數(shù)組不是把整個(gè)對象存在數(shù)組中 ,而是把指針存到內(nèi)存中.
-----------------------------------------------------------------------------------------------------------------
? ? ? ? NSArray *nsArray = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
? ? ? ? //數(shù)組長度
? ? ? ? NSInteger count = [nsArray count];
? ? ? ? NSLog(@"count = %ld",(long)count);
? ? ? ? //判斷數(shù)組中是否包含對應(yīng)對象
? ? ? ? BOOLisHave = [nsArray containsObject:@"3"];
? ? ? ? if(isHave)
? ? ? ? ? ? NSLog(@"包含");
? ? ? ? else
? ? ? ? ? ? NSLog(@"不包含");
? ? ? ? //取出數(shù)組中首個(gè)元素
? ? ? ? NSString*firstStr = [nsArray?firstObject];
? ? ? ? NSLog(@"firstStr =%@",firstStr);
? ? ? ? //取出數(shù)組中最后一個(gè)元素
? ? ? ? NSString*lastStr = [nsArray lastObject];
? ? ? ? NSLog(@"lastStr = %@",lastStr);
? ? ? ? //取出數(shù)組中下標(biāo)為3的元素
?? ? ? ? NSString*threeStr = [nsArray objectAtIndex:3];
? ? ? ? NSLog(@"threeStr = %@",threeStr);
? ? ? ? //打印對應(yīng)元素的下標(biāo)
? ? ? ? NSInteger index = [nsArray indexOfObject:@"2"];
? ? ? ? NSLog(@"index =%ld",(long)index);
-----------------------------------------------------------------------------------------------------------------
//數(shù)組遍歷(1.基本的for循環(huán),通過下標(biāo)逐一取出查看.for in快速枚舉.3.枚舉器(迭代器))
? ? ? ? Person *p = [[Person alloc] init];
? ? ? ? p.personName=@"公孫離";
? ? ? ? NSArray*personArray = [[NSArray alloc]initWithObjects:@"a",@"b",p,@"c",nil];
? ? ? ? // c語言版本:c89,c95,c99
? ? ? ? // c 89 int i 需要生命到外面
? ? ? ? for(inti =0;i
? ? ? ? ? ? NSString*arrayStr = [nsArray objectAtIndex:i];
? ? ? ? ? ? NSLog(@"arrayStr =%@",arrayStr);
? ? ? ? }
? ? ? ? for(inti =0;i
?? ? ? ? ? id personStr = [personArray objectAtIndex:i];
? ? ? ? ? ? if([personStr isKindOfClass:[Person class]]){
? ? ? ? ? ? ? ? Person*person = (Person*)personStr;
? ? ? ? ? ? ? ? NSLog(@"personName2 =%@",person.personName);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? NSString*personArray2 = (NSString*)personStr;
? ? ? ? ? ? ? ? NSLog(@"personArray2=%@",personArray2);
? ? ? ? ? ? }
? ? ? ? }
-----------------------------------------------------------------------------------------------------------------
? ? ? ? //快速枚舉
? ? ? ? //迭代相同類型元素的數(shù)組
? ? ? ? for(NSString*personStr2innsArray) {
? ? ? ? ? ? NSLog(@"personStr2 =%@",personStr2);
? ? ? ? }
? ? ? ? //迭代不同類型元素的數(shù)組
? ? ? ? for(id personStr3 in personArray) {
? ? ? ? ? ? if([personStr3 isKindOfClass:[Person class]]) {
? ? ? ? ? ? ? ? Person*person = (Person*)personStr3;
? ? ? ? ? ? ? ? NSLog(@"personName3 =%@",person.personName);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? NSString*personArray3 = (NSString*)personStr3;
? ? ? ? ? ? ? ? NSLog(@"personArray3 =%@",personArray3);
? ? ? ? ? ? }
? ? ? ? }