iOS中NSArray数组常用处理方式

1. 数组的常用处理方式

//--------------------不可变数组
//1.数组的创建
NSString *s1 = @"zhangsan";
NSString *s2 = @"lisi";
NSString *s3 = @"wangwu";
//(1)
NSArray *array1 = [[NSArray alloc] initWithObjects:s1,s2,s3, nil];
NSLog(@"%@",array1); //等价于 array1.descripton
//(2)使用类方法创建
NSArray *array2 = [NSArray arrayWithObjects:s1,s2,s3, nil];
//(3)创建一个数组对象,同时往里面存入一元素
NSArray *array3 = [NSArray arrayWithObject:s1];
//(4)创建一个数组,此数组中的元素来自array1
NSArray *array4 = [NSArray arrayWithArray:array1];
NSLog(@"array4 = %@",array4);
//2.通过下标取元素
NSString *str1 = [array4 objectAtIndex:0];
//3.数组元素的个数
NSUInteger count = [array4 count]; //等价于:array4.count;
//4.判断数组中的是否包含某个元素
BOOL isContains = [array4 containsObject:@"zhangsan"];
NSLog(@"isContains:%d",isContains);
//5.查找某一个对象在数组中的下标位置
NSUInteger index = [array4 indexOfObject:@"wangwu"];
if (index == NSNotFound) {
NSLog(@"Not find elemnts");
} else {
NSLog(@"index = %ld",index);
}
//6.链接数组中的字符串(前提:数组中都是字符串)
NSString *joinString = [array4 componentsJoinedByString:@","];
NSLog(@"joinString = %@",joinString);
//7.访问数组最后一个元素
NSString *lastObj = [array4 lastObject]; //array4.lastObject
NSLog(@"lsatObj = %@",lastObj);
//8.再原路的数组后面添加一个元素
NSArray *array5 = [array4 arrayByAddingObject:@"zhaolia"];
NSLog(@"array5 = %@",array5);
//取对应下标元素
int idx=4;
if (idx <array5.count) {
NSString *s = [array5 objectAtIndex:idx];
NSLog(@"s = %@",s);
}
//--------------数组的遍历
//1.普通遍历
for (int i=0; i<array5.count; i++) {
NSString *str = [array5 objectAtIndex:i];
NSLog(@"%@",str);
}
//快速遍历
for (NSString *s in array5) {
NSLog(@"%@",s);
}
//4.4之后的优化
//1.创建一个素组
NSArray *array7 = @[s1,s2,s3]; //等价于:NSArray *array7 = [NSArray arrayWithObjects:s1,s2,s3,nil];
NSLog(@"array7 = %@",array7);
NSString *str = array7[0];
NSLog(@"array[7] = %@",str);
//------------------可变数组
//初始化,设定元素个数为5,可以更改的。(继承自NSArray)
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:5];
//想数组中添加一个元素
[mutableArray addObject:@"aaa"];
//向数组中指定下标插入元素
[mutableArray insertObject:@"ccc" atIndex:0];
NSLog(@"%@",mutableArray); //原来位置元素后移
//移除最后一个元素
[mutableArray removeLastObject];
NSLog(@"移除最后元素后:%@",mutableArray);
//移除指定元素
[mutableArray removeObject:@"aaa"];
//移除指定下标元素
[mutableArray removeObjectAtIndex:0];
//向数组中添加数组
[mutableArray addObjectsFromArray:array1];
//1.创建可变数组
NSString *t1 = @"zhangsan ";
NSString *t2 = @"lisi";
NSString *t3 = @"wangwu ";
// NSMutableArray *mArray1 = @[s1,s2,s3];//wrong.这里创建了一个不可变数组
NSMutableArray *mArray1 = [[NSMutableArray alloc] initWithObjects:s1,s2,s3, nil];
//创建数组时,开辟3个空间用于存储元素,存储超过容量,数组自动增大空间
NSMutableArray *mArray2 = [[NSMutableArray alloc] initWithCapacity:3];
NSMutableArray *mArray3 = [NSMutableArray arrayWithCapacity:3];
//2.添加元素
[mArray2 addObject:t1];
[mArray2 addObject:t2];
[mArray2 addObject:t3];
NSLog(@"mArray2= %@",mArray2);
//将mArray2 中元素添加到mArray3中
// [mArray3 addObjectsFromArray:mArray2];
//将mArray2作为二维数字添加
[mArray3 addObject:mArray2];
NSLog(@"mArray3 = %@",mArray3);
//3.插入元素
[mArray2 insertObject:@"Jack" atIndex:0];
NSLog(@"mArray2 = %@",mArray2);
//4.替换元素
[mArray2 replaceObjectAtIndex:0 withObject:@"John"];
NSLog(@"替换:%@",mArray2);
//5.互换两个元素的位置
[mArray2 exchangeObjectAtIndex:3 withObjectAtIndex:0];
NSLog(@"mArray2 = %@",mArray2);
//6.删除元素
//6.1根据下标删除
[mArray2 removeObjectAtIndex:2];
NSLog(@"mArray2 = %@",mArray2);
//6.2删除最后一个元素
[mArray2 removeLastObject];
NSLog(@"mArray2 = %@",mArray2);
//6.3删除指定的对象
//[mArray2 removeObject:@"zhangsan"];
//6.4删除所有元素
[mArray2 removeAllObjects];
NSLog(@"mArray2 = %@",mArray2);

以上所述是小编给大家介绍的iOS中NSArray数组常用处理方式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

代码技巧

转载请关注公众号:代码技巧 回复:授权

本文链接地址:https://www.oudahe.com/p/54216/