iOS中NSArray数组常用处理方式
程序员文章站
2023-12-17 14:08:10
1. 数组的常用处理方式
//--------------------不可变数组
//1.数组的创建
nsstring *s1 = @"zhangsan"...
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数组常用处理方式,希望对大家有所帮助