NSArray *fruitArray=[[NSArray alloc] initWithObjects:@"apple",@"banana",@"pear",nil];开辟一个不可变的数组
NSString *obj=[fruitArray objectAtIndex:1];
NSArray *names=@[@"tom",@"jack",@"maggie"];
NSMutableArray *languages=[NSMutableArray array];
[languages addObject:@"c++"];
[languages addObject:@"Objc"]; 添加元素
[languages insertObject:@"swift" atIndex:1];
[languages removeObject:@"c++"];
[languages removeAllObjects];
[languages removeObjectAtIndex:0];
[languages replaceObjectAtIndex:0 withObject:@"c#"];
1 数组: 2 //1.不可变数组 3 // NSArray *fruitArr = [[NSArray alloc]initWithObjects:@"apple",@"banana",@"pear",@"watermelon",@"longan",nil]; 4 // NSLog(@"%@",fruitArr); 5 // NSLog(@"%lu",[fruitArr count]); 6 // NSString *obj = [fruitArr objectAtIndex:2]; 7 // NSLog(@"%@",obj); 8 // 9 // NSArray *appleArr = [NSArray arrayWithObjects:@"apple",@"banana",@"pear",nil]; 10 // NSLog(@"%@",appleArr); 11 // 12 // NSArray *names = @[@"tom",@"jack",@"kitty"]; 13 // NSLog(@"%@",names); 14 15 //2.可变数组 16 NSMutableArray *languages=[NSMutableArray array]; 17 [languages addObject:@"c++"]; 18 [languages addObject:@"oc "]; 19 [languages addObject:@"java"]; 20 [languages addObject:@"php"]; 21 NSLog(@"%@",languages); 22 [languages insertObject:@"swift" atIndex:1]; 23 NSLog(@"%@",languages); 24 [languages removeObject:@"php"]; 25 [languages removeObjectAtIndex:0]; 26 [languages replaceObjectAtIndex:1 withObject:@"c#"]; 27 NSLog(@"%@",languages); 28 29 //数组的遍历 30 // for (int i=0; i<[languages count]; i++) { 31 // NSLog(@"第%d个对象:%@",i+1,[languages objectAtIndex:i]); 32 // } 33 // 34 for (NSString *obj in languages) { 35 NSLog(@"%@",obj); 36 }
ie